java 此类可帮助您将SD卡中的图像异步加载到列表视图中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 此类可帮助您将SD卡中的图像异步加载到列表视图中相关的知识,希望对你有一定的参考价值。

import java.lang.ref.WeakReference;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.widget.ImageView;
 
/*
 *Original code i found from the below given link
 *---------------------------------------
 *http://www.samcoles.co.uk/tag/listview/
 *
 *How to use
 *----------
 *private final SDImageLoader mImageLoader = new SDImageLoader();
 *
 *mImageLoader.load(photoFilePath, photoView);
 *
 */

public class SDImageLoader {
 
    public void load(String filePath, ImageView v) {
        if(cancelPotentialSDLoad(filePath, v)) {
            SDLoadImageTask task = new SDLoadImageTask(v);
            SDLoadDrawable sdDrawable = new SDLoadDrawable(task);
            v.setImageDrawable(sdDrawable);
            task.execute(filePath);
        }
    }
 
    private Bitmap loadImageFromSDCard(String filePath) {
 
        BitmapFactory.Options bfo = new BitmapFactory.Options();
        bfo.inSampleSize = 4;
        bfo.outWidth = 150;
        bfo.outHeight = 150;
        Bitmap photo = BitmapFactory.decodeFile(filePath, bfo);
        return photo;
    } 
 
    private static boolean cancelPotentialSDLoad(String filePath, ImageView v) {
        SDLoadImageTask sdLoadTask = getAsyncSDLoadImageTask(v);
 
        if(sdLoadTask != null) {
            String path = sdLoadTask.getFilePath();
            if((path == null) || (!path.equals(filePath))) {
                sdLoadTask.cancel(true);
            } else {
                return false;
            }
        }
        return true;
    }
 
    private static SDLoadImageTask getAsyncSDLoadImageTask(ImageView v) {
        if(v != null) {
            Drawable drawable = v.getDrawable();
            if(drawable instanceof SDLoadDrawable) {
                SDLoadDrawable asyncLoadedDrawable = (SDLoadDrawable)drawable;
                return asyncLoadedDrawable.getAsyncSDLoadTask();
            }
        }
        return null;
    }
 
    private class SDLoadImageTask extends AsyncTask<String, Void, Bitmap> {
 
        private String mFilePath;
        private final WeakReference<ImageView> mImageViewReference;
 
        public String getFilePath() {
            return mFilePath;
        }
 
        public SDLoadImageTask(ImageView v) {
            mImageViewReference = new WeakReference<ImageView>(v);
        }
 
        @Override
        protected void onPostExecute(Bitmap bmp) {
            if(mImageViewReference != null) {
                ImageView v = mImageViewReference.get();
                SDLoadImageTask sdLoadTask = getAsyncSDLoadImageTask(v);
                // Change bitmap only if this process is still associated with it
                if(this == sdLoadTask) {
                    v.setImageBitmap(bmp);
                }
            }
        }
 
        @Override
        protected Bitmap doInBackground(String... params) {
            mFilePath = params[0];
            return loadImageFromSDCard(mFilePath);
        }
    }
 
    private class SDLoadDrawable extends ColorDrawable {
        private final WeakReference<SDLoadImageTask> asyncSDLoadTaskReference;
 
        public SDLoadDrawable(SDLoadImageTask asyncSDLoadTask) {
            super(Color.BLACK);
            asyncSDLoadTaskReference = new WeakReference<SDLoadImageTask>(asyncSDLoadTask);
        }
 
        public SDLoadImageTask getAsyncSDLoadTask() {
            return asyncSDLoadTaskReference.get();
        }
 
    }
}

以上是关于java 此类可帮助您将SD卡中的图像异步加载到列表视图中的主要内容,如果未能解决你的问题,请参考以下文章

从 SD 卡中获取图像,压缩,然后将其保存回 SD?

如何在android中获取存储在sd卡中的文件名

重命名 SD 卡中的文件夹

如何将可绘制资源中的图像保存到 SD 卡?

通过AsyncTask下载图像并将其显示在Imageview中而不存储在SD卡中

如何在存储 SD 卡之前裁剪捕获的图像? [复制]