android缓存SD卡中的图像
我想从listview项目中的SD卡中获取图像我可以显示图像,但我想缓存该图像,以便平滑滚动。有人能告诉我如何缓存SD卡中的图像吗?我得到了代码,但它显示了drawable中的图像,但我想从SD卡中获取图像android缓存SD卡中的图像,android,Android,我想从listview项目中的SD卡中获取图像我可以显示图像,但我想缓存该图像,以便平滑滚动。有人能告诉我如何缓存SD卡中的图像吗?我得到了代码,但它显示了drawable中的图像,但我想从SD卡中获取图像 import java.lang.ref.WeakReference; import java.util.ArrayList; import android.content.Context; import android.content.res.Resources; import andr
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.LruCache;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridLayout.LayoutParams;
import android.widget.GridView;
import android.widget.ImageView;
public class ListAdapter extends BaseAdapter {
Context context;
ArrayList<String> items;
private LruCache<String, Bitmap> mMemoryCache;
public ListAdapter(Context context, ArrayList<String> items) {
this.context = context;
this.items = items;
// Get memory class of this device, exceeding this amount will throw an
// OutOfMemory exception.
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in bytes rather than number
// of items.
return bitmap.getByteCount();
}
};
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int arg0) {
return items.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int arg0, View convertView, ViewGroup arg2) {
ImageView img = null;
if (convertView == null) {
img = new ImageView(context);
img.setScaleType(ImageView.ScaleType.CENTER_CROP);
img.setLayoutParams(new GridView.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
} else {
img = (ImageView) convertView;
}
int resId = context.getResources().getIdentifier(items.get(arg0),
"drawable", context.getPackageName());
loadBitmap(resId, img);
return img;
}
public void loadBitmap(int resId, ImageView imageView) {
if (cancelPotentialWork(resId, imageView)) {
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
imageView.setBackgroundResource(R.drawable.empty_photo);
task.execute(resId);
}
}
static class AsyncDrawable extends BitmapDrawable {
private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
public AsyncDrawable(Resources res, Bitmap bitmap,
BitmapWorkerTask bitmapWorkerTask) {
super(res, bitmap);
bitmapWorkerTaskReference = new WeakReference<BitmapWorkerTask>(
bitmapWorkerTask);
}
public BitmapWorkerTask getBitmapWorkerTask() {
return bitmapWorkerTaskReference.get();
}
}
public static boolean cancelPotentialWork(int data, ImageView imageView) {
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
if (bitmapWorkerTask != null) {
final int bitmapData = bitmapWorkerTask.data;
if (bitmapData != data) {
// Cancel previous task
bitmapWorkerTask.cancel(true);
} else {
// The same work is already in progress
return false;
}
}
// No task associated with the ImageView, or an existing task was
// cancelled
return true;
}
private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
if (imageView != null) {
final Drawable drawable = imageView.getDrawable();
if (drawable instanceof AsyncDrawable) {
final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
return asyncDrawable.getBitmapWorkerTask();
}
}
return null;
}
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
public Bitmap getBitmapFromMemCache(String key) {
return (Bitmap) mMemoryCache.get(key);
}
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
public int data = 0;
private final WeakReference<ImageView> imageViewReference;
public BitmapWorkerTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage
// collected
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background.
@Override
protected Bitmap doInBackground(Integer... params) {
data = params[0];
final Bitmap bitmap = decodeSampledBitmapFromResource(
context.getResources(), data, 100, 100);
addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
return bitmap;
}
// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
public static Bitmap decodeSampledBitmapFromResource(Resources res,
int resId, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
}
import java.lang.ref.WeakReference;
导入java.util.ArrayList;
导入android.content.Context;
导入android.content.res.Resources;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.graphics.drawable.BitmapDrawable;
导入android.graphics.drawable.drawable;
导入android.os.AsyncTask;
导入android.util.LruCache;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.GridLayout.LayoutParams;
导入android.widget.GridView;
导入android.widget.ImageView;
公共类ListAdapter扩展了BaseAdapter{
语境;
数组列表项;
私人LruCache-mMemoryCache;
公共ListAdapter(上下文、ArrayList项){
this.context=上下文;
这个项目=项目;
//获取此设备的内存类,超过此数量将抛出
//OutOfMemory异常。
final int maxMemory=(int)(Runtime.getRuntime().maxMemory()/1024);
//将可用内存的1/8用于此内存缓存。
最终int cacheSize=maxMemory/8;
mMemoryCache=新的LruCache(缓存大小){
受保护的int-sizeOf(字符串键、位图){
//缓存大小将以字节而不是数字来度量
//物品的数量。
返回bitmap.getByteCount();
}
};
}
@凌驾
public int getCount(){
返回items.size();
}
@凌驾
公共对象getItem(int arg0){
返回项目。获取(arg0);
}
@凌驾
公共长getItemId(int arg0){
返回arg0;
}
@凌驾
公共视图getView(int arg0、视图转换视图、视图组arg2){
ImageView img=null;
if(convertView==null){
img=新图像视图(上下文);
img.setScaleType(ImageView.ScaleType.CENTER\U裁剪);
img.setLayoutParams(新的GridView.LayoutParams(
LayoutParams.MATCH_父级,LayoutParams.MATCH_父级);
}否则{
img=(ImageView)convertView;
}
int resId=context.getResources().getIdentifier(items.get(arg0),
“可绘制”,context.getPackageName();
加载位图(剩余、img);
返回img;
}
公共void加载位图(int-resId、ImageView-ImageView){
if(取消潜在工作(剩余,图像视图)){
最终BitmapWorkerTask任务=新的BitmapWorkerTask(imageView);
imageView.setBackgroundResource(R.drawable.empty_photo);
任务执行(resId);
}
}
静态类AsyncDrawable扩展了BitmapDrawable{
私有最终WeakReference位图工作区taskreference;
公共异步绘制(资源分辨率、位图、,
位图工作任务(位图工作任务){
超级(分辨率,位图);
bitmapWorkerTaskReference=新的WeakReference(
位图任务);
}
公共BitmapWorkerTask getBitmapWorkerTask(){
返回bitmapWorkerTaskReference.get();
}
}
公共静态布尔取消潜在工作(int数据,ImageView){
最终BitmapWorkerTask BitmapWorkerTask=getBitmapWorkerTask(图像视图);
if(bitmapWorkerTask!=null){
final int bitmapData=bitmapWorkerTask.data;
if(位图数据!=数据){
//取消以前的任务
bitmapWorkerTask.cancel(真);
}否则{
//同样的工作已经在进行中
返回false;
}
}
//没有与ImageView关联的任务,或者未找到现有任务
//取消
返回true;
}
私有静态BitmapWorkerTask getBitmapWorkerTask(ImageView ImageView){
如果(imageView!=null){
final Drawable Drawable=imageView.getDrawable();
if(异步可绘制的可绘制实例){
最终AsyncDrawable AsyncDrawable=(AsyncDrawable)drawable;
返回asyncDrawable.getBitmapWorkerTask();
}
}
返回null;
}
public void addBitmapToMemoryCache(字符串键、位图){
if(getBitmapFromMemCache(key)==null){
mMemoryCache.put(键,位图);
}
}
公共位图getBitmapFromMemCache(字符串键){
返回(位图)mMemoryCache.get(键);
}
类BitmapWorkerTask扩展了AsyncTask{
公共整数数据=0;
私有最终WeakReference imageViewReference;
公共位图工作任务(ImageView ImageView){
//使用WeakReference确保ImageView可以是垃圾
//收集
imageViewReference=新的WeakReference(imageView);
}
//在背景中解码图像。
@凌驾
受保护位图doInBackground(整数…参数){
数据=参数[0];
最终位图位图=decodeSampledBitmapFromResource(
getResources(),数据,100100);
addBitmapToMemoryCache(String.valueOf(params[0]),位图);
返回位图;
}
//完成后,查看ImageView是否仍然存在并设置位图。
@凌驾
受保护的void onPostExecute(位图){
if(imageViewReference!=null&&bitmap!=null){
最终ImageView=imageViewReference.get();
如果(imageView!=null){
设置图像位图(位图);
}
}
}
}
公共静态位图解码SampledBitMapFromResource,
int r
AQuery aQuery=new AQuery(this);
File file = new File(YOUR IMAGE PATH);
aq.id(R.id.avatar).image(file, true,true); // that both true means you caching your images.