也许有很多人会遇到一个图片加载不出来的时候, 有这么一个现象,当你第一次可以加载到图片,但是到第二次就加载不出来了,这个问题主要原因是图片读取问题,我现在把我做的例子分享给大家,希望对大家有帮助...
main.xml布局文件文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cn"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".AsyncImageViewActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
2,AsyncImageViewActivity类
package com.cn;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
public class AsyncImageViewActivity extends Activity {
private ImageView user_image; //用户头像
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
user_image = (ImageView)findViewById(R.id.user_image);
String image_url = "http://fs.yy365.com/d3/photos/2010/05/20/09/pic_1274317680739_a637337f-0769-4cb3-ad07-bb2b1ba38c4a_1.jpg";
//异步加载头像
AsyncImageLoader.setImageViewFromUrl(image_url, user_image);
}
}
3,对于图片异步处理类:AsyncImageLoader ,这个类我采用了缓存.提高图片的访问效率,其中图片的读取方式被我修改了,只要不会出现图片读取出错的情况.
package com.cn;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.HashMap;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
/**
* 对于图片异步处理类
* @author liuxr
*
*/
public class AsyncImageLoader {
static ImageView singImageView; //针对于单张图片异步加载
private static HashMap<String, SoftReference<Drawable>> singleImageCache = null;
/**
* 通过图片地址,返回drawable
* @param url
* @return
*/
public static Drawable loadImageFromUrl(String url) {
ByteArrayOutputStream out = null;
Drawable drawable = null;
int BUFFER_SIZE = 1024*16;
InputStream inputStream = null;
try{
inputStream = new URL(url).openStream();
BufferedInputStream in = new BufferedInputStream(inputStream, BUFFER_SIZE);
out = new ByteArrayOutputStream(BUFFER_SIZE);
int length = 0;
byte[] tem = new byte[BUFFER_SIZE];
length = in.read(tem);
while(length != -1){
out.write(tem, 0, length);
length = in.read(tem);
}
in.close();
drawable = Drawable.createFromStream(new ByteArrayInputStream(out.toByteArray()), "src");
}catch(Exception e){
e.printStackTrace();
}finally{
if(inputStream != null){
try{
inputStream.close();
}catch(Exception e){}
}
}
return drawable;
}
/**
* 异步设置单张imageview图片,采取软引用
* @param url 网络图片地址
* @param imageView 需要设置的imageview
*/
public static void setImageViewFromUrl(final String url, final ImageView imageView){
singImageView = imageView;
//如果软引用为空,就新建一个
if(singleImageCache == null){
singleImageCache = new HashMap<String, SoftReference<Drawable>>();
}
//如果软引用中已经有了相同的地址,就从软引用中获取
if(singleImageCache.containsKey(url)){
SoftReference<Drawable> soft = singleImageCache.get(url);
Drawable draw = soft.get();
singImageView.setImageDrawable(draw);
return;
}
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
singImageView.setImageDrawable((Drawable)msg.obj);
}
};
//子线程不能更新UI,又不然会报错
new Thread(){
public void run() {
Drawable drawable = loadImageFromUrl(url);
if(drawable == null){
Log.e("single imageview", "single imageview of drawable is null");
}else{
//把已经读取到的图片放入软引用
singleImageCache.put(url, new SoftReference<Drawable>(drawable));
}
Message message = handler.obtainMessage(0, drawable);
handler.sendMessage(message);
};
}.start();
}
}
4,AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cn"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".AsyncImageViewActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>