Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canvas: trying to use a recycled bitmap android

Tags:

I am continously having this problem and I don't know what to do about it.

I've used this library and when I get the cropped image I save it in a static variable and move to the next activity. When I arrive in the next activity , I reference that static variable to get the bitmap and try to scale it down. But it gives me error.

Here's what I am doing.

public void buttonCropClick(View view) throws IOException {     imageView.setDrawingCacheEnabled(true);     imageView.buildDrawingCache(true);     Snapshot.CroppedBitmap = imageView.getDrawingCache(true);     imageView.setDrawingCacheEnabled(false);     startActivity(new Intent(this,RecommendationInfo.class)); } 

in the RecommendationInfo class , I get the bitmap in the following line Snapshot.CroppedBitmap = imageView.getDrawingCache(true); then I save this bitmap in the static variable which I reference in next activity and pass it to the following function.

public static Bitmap scaleDown(Bitmap realImage,boolean filter) {      float maxImageSize = HeightToSet;     float ratio = Math.min(             (float) maxImageSize / realImage.getWidth(),             (float) maxImageSize / realImage.getHeight());     int width = Math.round((float) ratio * realImage.getWidth());     int height = Math.round((float) ratio * realImage.getHeight());      // Error here     Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width,height, filter);     return newBitmap; } 

I already tried calling bitmap.recycle(). Why am I getting this problem what can I do to solve it ? Here's my logcat.

07-14 03:09:43.713: E/AndroidRuntime(19653): FATAL EXCEPTION: main 07-14 03:09:43.713: E/AndroidRuntime(19653): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@4059b8b8 07-14 03:09:43.713: E/AndroidRuntime(19653):    at android.graphics.Canvas.throwIfRecycled(Canvas.java:955) 07-14 03:09:43.713: E/AndroidRuntime(19653):    at android.graphics.Canvas.drawBitmap(Canvas.java:1012) 07-14 03:09:43.713: E/AndroidRuntime(19653):    at android.graphics.Bitmap.createBitmap(Bitmap.java:462) 07-14 03:09:43.713: E/AndroidRuntime(19653):    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349) 07-14 03:09:43.713: E/AndroidRuntime(19653):    at com.example.Libraries.Snapshot.scaleDown(Snapshot.java:42) 07-14 03:09:43.713: E/AndroidRuntime(19653):    at com.example.androidtestproject.RecommendationInfo.SetRecommendationValues(RecommendationInfo.java:195) 07-14 03:09:43.713: E/AndroidRuntime(19653):    at com.example.androidtestproject.RecommendationInfo.access$5(RecommendationInfo.java:183) 07-14 03:09:43.713: E/AndroidRuntime(19653):    at com.example.androidtestproject.RecommendationInfo$1.onClick(RecommendationInfo.java:154) 07-14 03:09:43.713: E/AndroidRuntime(19653):    at android.view.View.performClick(View.java:2552) 07-14 03:09:43.713: E/AndroidRuntime(19653):    at android.view.View$PerformClick.run(View.java:9229) 07-14 03:09:43.713: E/AndroidRuntime(19653):    at android.os.Handler.handleCallback(Handler.java:587) 07-14 03:09:43.713: E/AndroidRuntime(19653):    at android.os.Handler.dispatchMessage(Handler.java:92) 07-14 03:09:43.713: E/AndroidRuntime(19653):    at android.os.Looper.loop(Looper.java:138) 07-14 03:09:43.713: E/AndroidRuntime(19653):    at android.app.ActivityThread.main(ActivityThread.java:3701) 07-14 03:09:43.713: E/AndroidRuntime(19653):    at java.lang.reflect.Method.invokeNative(Native Method) 07-14 03:09:43.713: E/AndroidRuntime(19653):    at java.lang.reflect.Method.invoke(Method.java:507) 07-14 03:09:43.713: E/AndroidRuntime(19653):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878) 07-14 03:09:43.713: E/AndroidRuntime(19653):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636) 07-14 03:09:43.713: E/AndroidRuntime(19653):    at dalvik.system.NativeStart.main(Native Method) 07-14 03:09:45.515: E/TAG(20039): End of input at character 0 of  
like image 435
Mj1992 Avatar asked Jul 13 '13 22:07

Mj1992


1 Answers

Copy the bitmap before you pass it to the static variable.

Snapshot.CroppedBitmap = imageView.getDrawingCache(true); 

to

Snapshot.CroppedBitmap = Bitmap.createBitmap(imageView.getDrawingCache(true)); 

It is very likely that the imageView is recycling its drawing cache when no longer needed as good practice. By copying it out, you keep a reference to the bitmap the ImageView trashed. Just make a copy of it that you can manage yourself!

like image 64
AStupidNoob Avatar answered Nov 29 '22 07:11

AStupidNoob