1.MainActivity

package com.example.httpimagedemo;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.widget.ImageView;public class MainActivity extends Activity {private static final String PATH = "http://img1.imgtn.bdimg.com/it/u=3199407981,256809178&fm=23&gp=0.jpg";private ImageView img = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);img = (ImageView) findViewById(R.id.img);new Thread(new mThread()).start();}private class mThread implements Runnable {@Overridepublic void run() {try {byte data[] = getUrlData();Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length); // 生成图片img.setImageBitmap(bm);} catch (Exception e) {e.printStackTrace();}}}public byte[] getUrlData() throws Exception {ByteArrayOutputStream bos = null; // 内存输出流try {URL url = new URL(PATH);bos = new ByteArrayOutputStream();byte data[] = new byte[1024]; // 每次读取1024HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 打开InputStream input = conn.getInputStream(); // 取得输入流int len = 0; // 读取的长度while ((len = input.read(data)) != -1) { // 如果没有的读取到底部(如果是底部的话,会返回-1),则继续读取bos.write(data, 0, len); // 向内存中保存}return bos.toByteArray();// 变为字节数组返回} catch (Exception e) {throw e;} finally {bos.close();// 关闭输入流}}}

2.activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <ImageView        android:id="@+id/img"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></RelativeLayout>

3.AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.httpimagedemo"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <uses-permission android:name="android.permission.INTERNET" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.httpimagedemo.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>


更多相关文章

  1. Android(安卓)Studio入门小例子
  2. Android获取CPU,内存等系统信息方法
  3. 输入处理 Input Processor
  4. android ZRAM
  5. Build Android(安卓)Studio development environment in Linux
  6. android 4中文件操作
  7. Android中SharedPreference多进程数据共享出错
  8. android 多个cursor 的读取 和读取某个Cursor其他从集合中获取的
  9. android 客制化系统签名文件

随机推荐

  1. Android开发环境搭建
  2. Android(安卓)控件 ListView
  3. Android架构分析之Android开发环境搭建
  4. Android(安卓)小的知识片
  5. Android异步处理四:AsyncTask的实现原理
  6. android ListView嵌套checkbox并取代chec
  7. Android重要控件概览(上)
  8. Android子控件超出父控件方法
  9. android 5.0 默认水波纹背景属性,可设置
  10. Android(安卓)DrawerLayout 侧边栏使用之