我不断有这个问题,我不知道该怎么办。画布:试图使用回收的位图android
我已经使用this库,当我得到裁剪后的图像时,我将它保存在一个静态变量中并移动到下一个活动。当我到达下一个活动时,我引用该静态变量来获取位图并尝试将其缩小。但它给了我错误。
这是我正在做的。
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));
}
在RecommendationInfo
类,我得到以下行Snapshot.CroppedBitmap = imageView.getDrawingCache(true);
位图,然后我保存这个位图中,我在接下来的活动中引用,并把它传递给下面的函数静态变量。
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;
}
我已经试过拨打电话bitmap.recycle()
。为什么我会遇到这个问题我该如何解决这个问题?这是我的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 [email protected]
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
它给了你什么错误? – Alamri
我更新了这个问题,请检查 – Mj1992
你的位图是在某个地方回收的,你回收后不能调用位图。请在开始其他活动之前查看是否回收位图。 – k0sh