2
我一直試圖通過MediaStore內容提供商查詢SD卡上的所有圖像,並在GridView上顯示它們的縮略圖。使用asynctask從內容提供商加載圖像位圖
但是,如果我加載主線程上的圖像的縮略圖,滾動得慢得令人難以置信...
所以我試圖加載通過asynctasks位圖: 滾動性能變得更好,但現在的電網項目保持重裝他們的縮略圖,直到它得到正確的位圖...
這裏是我的AsyncTask,它加載位圖:
package x.y;
import java.lang.ref.WeakReference;
import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory.Options;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.widget.ImageView;
public class ImageThumbnailLoader extends AsyncTask<Long, Void, Bitmap> {
private final Options mOptions;
private WeakReference<ImageView> mImageViewWeakReference;
private ContentResolver mContentResolver;
public ImageThumbnailLoader(ImageView imageView,
ContentResolver cr) {
mContentResolver = cr;
mImageViewWeakReference = new WeakReference<ImageView>(imageView);
mOptions = new Options();
mOptions.inSampleSize = 4;
}
@Override
protected Bitmap doInBackground(Long... params) {
Bitmap result;
result = MediaStore.Images.Thumbnails.getThumbnail(
mContentResolver, params[0],
MediaStore.Images.Thumbnails.MINI_KIND, mOptions);
return result;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if (mImageViewWeakReference != null
&& mImageViewWeakReference.get() != null)
mImageViewWeakReference.get().setImageBitmap(result);
}
}
這裏是我的自定義光標適配器:
package x.y;
import android.content.Context;
import android.database.Cursor;
import android.graphics.BitmapFactory.Options;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
public class MediaCursorAdapter extends CursorAdapter {
private LayoutInflater mInflater;
private final static int mColumnID = 0;
private Options mOptions;
public MediaCursorAdapter(Context context, Cursor c) {
super(context, c);
mInflater = LayoutInflater.from(context);
mOptions = new Options();
mOptions.inSampleSize = 4;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder) view.getTag();
ImageThumbnailLoader imageLoader = new ImageThumbnailLoader(holder.thumbImg,
context.getContentResolver());
imageLoader.execute(cursor.getLong(mColumnID));
// holder.thumbImg.setImageBitmap(MediaStore.Images.Thumbnails.getThumbnail(
// context.getContentResolver(), cursor.getLong(mColumnID),
// MediaStore.Images.Thumbnails.MINI_KIND, mOptions));
Log.i("Prototype", "bindView : " + cursor.getPosition());
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Log.i("Prototype", "newView : " + cursor.getPosition());
View view = mInflater.inflate(R.layout.grid_item, null);
ViewHolder holder = new ViewHolder(view);
view.setTag(holder);
return view;
}
private static class ViewHolder {
ImageView thumbImg, dragImg;
ViewHolder(View base) {
thumbImg = (ImageView) base.findViewById(R.id.thumbImage);
dragImg = (ImageView) base.findViewById(R.id.dragImage);
}
}
}
我查詢光標與此代碼,並將其發送到適配器:
query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATA }, null, null,
MediaStore.Images.Media._ID);
貌似在我的定義光標適配器bindview()被調用往往比它應該是...任何人都知道我怎樣才能讓我的GridView的圖像停止重新加載,同時保持滾動性能?
在此先感謝。