下面是在doInBackground()AsynchTask中从MediaStore.Images.Thumbnails.getThumbnail创建超过4000个缩略图图像的代码。现在我在Android 2.3.6中执行。它没有在我得到 RuntimeException 之间创建缩略图。
private void loadImages() {
try {
columns = new String[] { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID, MediaStore.Images.Media.SIZE,
MediaStore.Images.Media.TITLE };
orderBy = new String(MediaStore.Images.Media._ID);
fileCursor = getActivity().managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
null, null, orderBy);
image_column_index = fileCursor
.getColumnIndex(MediaStore.Images.Media._ID);
this.imageCount = fileCursor.getCount();
this.thumbnails = new Bitmap[this.imageCount];
this.arrPath = new String[this.imageCount];
GlobalValues.IMAGE_ARR_PATH = new String[this.imageCount];
GlobalValues.IMAGE_THUMBNAILSSELECTION = new boolean[this.imageCount];
GlobalValues.IMAGES_SIZE = new String[this.imageCount];
this.thumbnailsselection = new boolean[this.imageCount];
if (GlobalValues.IMAGE_SELECTION != null) {
thumbnailsselection = GlobalValues.IMAGE_SELECTION;
GlobalValues.IMAGE_THUMBNAILSSELECTION = thumbnailsselection;
}
int inSample = 1;
BitmapFactory.Options opts=null;
System.out.println("imageCount:"+imageCount);
for (int i = 0; i < this.imageCount; i++) {
fileCursor.moveToPosition(i);
int id = fileCursor.getInt(image_column_index);
int dataColumnIndex = fileCursor
.getColumnIndex(MediaStore.Images.Media.DATA);
arrPath[i] = fileCursor.getString(dataColumnIndex);
GlobalValues.IMAGE_ARR_PATH[i] = fileCursor
.getString(dataColumnIndex);
GlobalValues.IMAGES_SIZE[i] = fileCursor.getString(fileCursor
.getColumnIndex(MediaStore.Images.Media.SIZE));
opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
opts.inSampleSize = inSample;
if (Build.VERSION.SDK_INT > 11) {
bMap = MediaStore.Images.Thumbnails.getThumbnail(
context.getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, opts);
if(bMap != null && !bMap.isRecycled())
thumbnails[i]= bMap;
} else {
if (arrPath[i].endsWith(".png")) {
bMap = BitmapFactory.decodeFile(arrPath[i],opts);
bMap = ThumbnailUtils.extractThumbnail(
bMap, 96, 96);
if(bMap != null && !bMap.isRecycled() )
thumbnails[i]= bMap;
} else{
System.out.println("index:"+i);
//Getting Error in below code
bMap = MediaStore.Images.Thumbnails
.getThumbnail(
context.getContentResolver(),
id,
MediaStore.Images.Thumbnails.MICRO_KIND,
(BitmapFactory.Options) opts);
System.gc();
Runtime.getRuntime().gc();
thumbnails[i]= bMap;
if (bMap != null && !bMap.isRecycled()) {
bMap.recycle();
bMap = null;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
fileCursor = null;
}
}
我得到以下异常:
07-03 12:40:37.269:E / AndroidRuntime(3185): java.lang.RuntimeException:Canvas:尝试使用循环位图 android.graphics.Bitmap@4060cc48 07-03 12:40:37.269: E / AndroidRuntime(3185):at android.graphics.Canvas.throwIfRecycled(Canvas.java:955)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.graphics.Canvas.drawBitmap(Canvas.java:1044)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:325) 07-03 12:40:37.269:E / AndroidRuntime(3185):at android.widget.ImageView.onDraw(ImageView.java:872)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.View.draw(View.java:6880)07-03 12:40:37.269: E / AndroidRuntime(3185):at android.view.ViewGroup.drawChild(ViewGroup.java:1646)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewGroup.drawChild(ViewGroup.java:1644)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.widget.AbsListView.dispatchDraw(AbsListView.java:1586)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.widget.GridView.dispatchDraw(GridView.java:1948)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.View.draw(View.java:6986)07-03 12:40:37.269: E / AndroidRuntime(3185):at android.widget.AbsListView.draw(AbsListView.java:2967)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewGroup.drawChild(ViewGroup.java:1646)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewGroup.drawChild(ViewGroup.java:1644)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewGroup.drawChild(ViewGroup.java:1644)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewGroup.drawChild(ViewGroup.java:1644)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.View.draw(View.java:6883)07-03 12:40:37.269: E / AndroidRuntime(3185):at android.support.v4.view.ViewPager.draw(ViewPager.java:1574)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewGroup.drawChild(ViewGroup.java:1646)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.View.draw(View.java:6883)07-03 12:40:37.269: E / AndroidRuntime(3185):at android.view.ViewGroup.drawChild(ViewGroup.java:1646)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewGroup.drawChild(ViewGroup.java:1644)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewGroup.drawChild(ViewGroup.java:1644)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewGroup.drawChild(ViewGroup.java:1644)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.View.draw(View.java:6883)07-03 12:40:37.269: E / AndroidRuntime(3185):at android.widget.FrameLayout.draw(FrameLayout.java:357)07-03 12:40:37.269:E / AndroidRuntime(3185):at com.android.internal.policy.impl.PhoneWindow $ DecorView.draw(PhoneWindow.java:1921) 07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewRoot.draw(ViewRoot.java:1528)07-03 12:40:37.269: E / AndroidRuntime(3185):at android.view.ViewRoot.performTraversals(ViewRoot.java:1264)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.view.ViewRoot.handleMessage(ViewRoot.java:1866)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.os.Handler.dispatchMessage(Handler.java:99)07-03 12:40:37.269:E / AndroidRuntime(3185):at android.os.Looper.loop(Looper.java:130)07-03 12:40:37.269: E / AndroidRuntime(3185):at android.app.ActivityThread.main(ActivityThread.java:3687)07-03 12:40:37.269:E / AndroidRuntime(3185):at java.lang.reflect.Method.invokeNative(Native Method)07-03 12:40:37.269:E / AndroidRuntime(3185):at java.lang.reflect.Method.invoke(Method.java:507)07-03 12:40:37.269: E / AndroidRuntime(3185):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867) 07-03 12:40:37.269:E / AndroidRuntime(3185):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)07-03 12:40:37.269:E / AndroidRuntime(3185):at dalvik.system.NativeStart.main(原生方法)
答案 0 :(得分:0)
您将位图存储在数组thumbnails[i]= bMap;
中。我想你以后用它们画画?
然后调用bMap.recycle();
来释放存储在本机内存中的位图数据。您只应在计划不再使用的位图上调用recycle();
。