Android 通过子线程加载URL图片
昨天下午折腾了一下午从SD卡、资源文件、网络加载图片的东西,一直折腾到下了班还再弄了一个小时,原谅我技术不咋地吧,反正临走之前,都拿项目里面了,也算是没给自己的大经理丢人吧!囧 通过URL加载网络上的图片时,我只是提供一种可行的方法,核心代码也是我从网上Copy的,自己改进了一下。
首先加上网络访问权限:
<uses-permission android:name="android.permission.INTERNET" />
刚开始,在网上找实例的时候,发现大部分示例都是在主线程中执行网络请求的,但是由于android 4.0以后不能在主线程进行网络访问了,因为可能会造成ANR,所以这里采用子线程进行网络访问请求,然后通过message,通知主线程更新UI。这只是网络上众多方法中的其中一个。可以参考这个连接:
android 4.0以上执行网络请求的方法
接下来贴代码:(大神请不要嘲笑本小菜鸟)
- activity_main.xml
<LinearLayout 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:background="@color/white"
android:orientation="vertical"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/img_edt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:hint="输入URL地址"
android:layout_toLeftOf="@+id/img_btn"
android:background="@drawable/shape_edt"
android:height="50dp" />
<Button
android:id="@+id/img_btn"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="3dp"
android:background="@drawable/shape"
android:text="确定"
android:textSize="18dp" />
</RelativeLayout>
<ImageView
android:id="@+id/imagevv"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
- MainActivity.java
package com.example.imageload;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends Activity {
private Button searchBtn;// 确定按钮
private ImageView img;// 图片容器
private EditText searchEdt;// 输入框
private LoadImageConnection conn;// 子线程
private Handler mhandler;
private String path;// URL路径
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchBtn = (Button) findViewById(R.id.img_btn);
img = (ImageView) findViewById(R.id.imagevv);
searchEdt = (EditText) findViewById(R.id.img_edt);
init();
}
private void init() {
// 接收message通知
mhandler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
img.setImageBitmap((Bitmap) msg.obj);
}
}
};
// 为按钮设置监听时间
searchBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
path = searchEdt.getText().toString();
conn = new LoadImageConnection(mhandler, path);
conn.start();// 启动子线程加载网络访问的图片
}
});
}
}
- LoadImageConnection.java 子线程
package com.googosoft.hhxy.LagerImage;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.Handler;
import android.os.Message;
public class LoadImageConnection extends Thread {
Handler mhandler;
String ImageUrl;
public LoadImageConnection(Handler mhandler, String ImageUrl) {
this.mhandler = mhandler;
this.ImageUrl = ImageUrl;
}
@Override
public void run() {
try {
URL url = new URL(ImageUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setRequestProperty(
"User-Agent",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Shuame)");
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
InputStream inputStream = conn.getInputStream();
// Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
// iv.setImageBitmap(bitmap);
// 采用传送消息的模式 把view操作消息发给主线程
Message msg = new Message();
msg.what = 0x123;
msg.obj = inputStream;
mhandler.sendMessage(msg);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- shape.xml 样式文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="15dp" />
<solid android:color="#2A9BE5" />
</shape>
- shape_btn.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="8px" />
<solid android:color="#FFFFFF" />
<stroke
android:width="2px"
android:color="#BFBFBF" />
</shape>
源码:http://download.youkuaiyun.com/detail/shuai_de_yi_ta_hu_tu/9411768