您的位置:首页 > 理论基础 > 计算机网络

android 加载网络图片

2016-08-31 16:29 387 查看
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:text="加载" />

<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

</LinearLayout>


网络类

package com.example.viewwebimagedemo.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtils {

private final static String URL_PATH = "http://images.cnblogs.com/cnblogs_com/liuning8023/588559/o_Android.jpg";

public HttpUtils() {
// TODO Auto-generated constructor stub
}

public static InputStream getImageViewInputStream() throws IOException {
InputStream inputStream = null;

URL url = new URL(URL_PATH);
if (url != null) {
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setDoInput(true);
int response_code = httpURLConnection.getResponseCode();
if (response_code == 200) {
inputStream = httpURLConnection.getInputStream();
}
}
return inputStream;
}
}


package com.example.viewwebimagedemo;

import java.io.InputStream;

import com.example.viewwebimagedemo.R;
import com.example.viewwebimagedemo.utils.HttpUtils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
private Button button;
private ImageView imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

button = (Button) this.findViewById(R.id.btn);
imageView = (ImageView) this.findViewById(R.id.imageView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(access).start();
}
});
}
// 另一个线程从网络获得图片
private Runnable access = new Runnable() {
@Override
public void run() {
getImg();
}
};

// 获得图片
public void getImg() {
try {
InputStream inputStream = HttpUtils.getImageViewInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Looper.prepare();// 必须调用此方法,要不然会报错
Message msg = new Message();
msg.what = 0;
msg.obj = bitmap;
handler.sendMessage(msg);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "获取图片错误", 1).show();
}
}

// 更新UI信息
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0) {
if (msg.obj == null) {
Toast.makeText(getApplicationContext(), "图片不存在", 1).show();
} else {
Toast.makeText(getApplicationContext(), "加载图片...", 1)
.show();
setImg((Bitmap) msg.obj);
}
}
}
};
// 加载图片
private void setImg(Bitmap bm) {
imageView.setImageBitmap(bm);
}
}


效果:



记得加入权限: <uses-permission android:name="android.permission.INTERNET" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: