无法包裹回收位图错误

时间:2012-12-05 13:12:47

标签: android

我正在尝试将图像添加到相机surfaceview,并尝试使用以下代码获取该图像。

       else if(v.equals(findViewById(R.id.ok_button))){
//                saveImage();

                topbar.setVisibility(View.GONE);
                menubar.setVisibility(View.GONE);
                bottom.setVisibility(View.GONE);

                View s = ml.getRootView();
                s.setDrawingCacheEnabled(true);

                Bitmap b = s.getDrawingCache();


                Bitmap watermarkimage=b;
                s.setVisibility(View.GONE);
                Log.e("ok","ok");
                Intent i=new Intent(CameraActivity.this,Update.class);
                 i.putExtra("data",watermarkimage);
                 startActivity(i);
                 finish();
                 }

我保存了整个活动以获取图像。通过使用以下代码,我得到了位图

 View s = ml.getRootView();
                s.setDrawingCacheEnabled(true);

                Bitmap b = s.getDrawingCache();

当我尝试将此位图发送到另一个活动时,我收到此错误

12-05 17:58:38.700: E/AndroidRuntime(10590): FATAL EXCEPTION: main
12-05 17:58:38.700: E/AndroidRuntime(10590): java.lang.IllegalStateException: Can't parcel a recycled bitmap
12-05 17:58:38.700: E/AndroidRuntime(10590):    at android.graphics.Bitmap.checkRecycled(Bitmap.java:210)
12-05 17:58:38.700: E/AndroidRuntime(10590):    at android.graphics.Bitmap.writeToParcel(Bitmap.java:960)
12-05 17:58:38.700: E/AndroidRuntime(10590):    at android.os.Parcel.writeParcelable(Parcel.java:1151)
12-05 17:58:38.700: E/AndroidRuntime(10590):    at android.os.Parcel.writeValue(Parcel.java:1070)
12-05 17:58:38.700: E/AndroidRuntime(10590):    at android.os.Parcel.writeMapInternal(Parcel.java:488)
12-05 17:58:38.700: E/AndroidRuntime(10590):    at android.os.Bundle.writeToParcel(Bundle.java:1552)
12-05 17:58:38.700: E/AndroidRuntime(10590):    at android.os.Parcel.writeBundle(Parcel.java:502)
12-05 17:58:38.700: E/AndroidRuntime(10590):    at android.content.Intent.writeToParcel(Intent.java:5492)
12-05 17:58:38.700: E/AndroidRuntime(10590):    at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1341)
12-05 17:58:38.700: E/AndroidRuntime(10590):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1374)
12-05 17:58:38.700: E/AndroidRuntime(10590):    at android.app.Activity.startActivityForResult(Activity.java:2873)

1 个答案:

答案 0 :(得分:5)

尝试复制或克隆此位图并将副本包裹起来。

Bitmap watermarkimage = b.copy(b.getConfig(), true);

我猜测getDrawingCache返回的数据与b和watermarkimage共享。所以也许setVisible to Gone会导致它回收。