也許有很多人會遇到一個圖片加載不出來的時候, 有這麼一個現象,當你第一次可以加載到圖片,但是到第二次就加載不出來了,這個問題主要原因是圖片讀取問題,我現在把我做的例子分享給大家,希望對大家有幫助...
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>