3

When imageView clicked, I want to display the image in full screen. For first time it works, but after that it crashed. And the error pointed to

startActivity(intent);

Code

Intent intent = new Intent(AddExpenses.this,FullScreenImage.class);
imageView.buildDrawingCache();
Bitmap image= imageView.getDrawingCache();
Bundle extras = new Bundle();
extras.putParcelable("imagebitmap", image);
intent.putExtras(extras);
startActivity(intent);
image.recycle();

FullScreenImage

public class FullScreenImage extends Activity {

    @SuppressLint("NewApi")

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.layout_full);
        Bundle extras = getIntent().getExtras();
        Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");
        ImageView imgDisplay;
        Button btnClose;

        imgDisplay = (ImageView) findViewById(R.id.imgDisplay);
        btnClose = (Button) findViewById(R.id.btnClose);
        btnClose.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                FullScreenImage.this.finish();
            }
        });
        imgDisplay.setImageBitmap(bmp );
    }
}

Logcat

E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: Can't parcel a recycled bitmap
            at android.graphics.Bitmap.checkRecycled(Bitmap.java:256)
            at android.graphics.Bitmap.writeToParcel(Bitmap.java:1366)
            at android.os.Parcel.writeParcelable(Parcel.java:1254)
            at android.os.Parcel.writeValue(Parcel.java:1173)

I use extras.putParcelable("imagebitmap",image.copy(image.getConfig(), true)); but getting new error

java.lang.IllegalStateException: Can't copy a recycled bitmap
            at android.graphics.Bitmap.checkRecycled(Bitmap.java:256)
            at android.graphics.Bitmap.copy(Bitmap.java:454)
            at com.example.tony.monthlyexpenses.AddExpenses$4.onClick(AddExpenses.java:176)
AI.
  • 934
  • 2
  • 14
  • 30
  • Remove `image.recycle();` line then check – ρяσѕρєя K Jan 03 '17 at 05:25
  • @ρяσѕρєяK It won't crash if I remove the line, but it will return the previous image, not the new one. FYI, I load the gallery image to imageView. – AI. Jan 03 '17 at 05:26
  • Why not just passing image URI to next screen instead of passing large bitmap object which will cause memory issue anytime – ρяσѕρєя K Jan 03 '17 at 05:34
  • Possible duplicate of [java.lang.IllegalStateException: Can't parcel a recycled bitmap error occurs](http://stackoverflow.com/questions/13737485/java-lang-illegalstateexception-cant-parcel-a-recycled-bitmap-error-occurs) – Harshad Pansuriya Jan 03 '17 at 05:38
  • @ρяσѕρєяK URI image will caused out of memory issues too > – AI. Jan 03 '17 at 05:45
  • Check out this following link. [Answer](http://stackoverflow.com/questions/13737485/java-lang-illegalstateexception-cant-parcel-a-recycled-bitmap-error-occurs) – Tulsi Jan 03 '17 at 05:50
  • Check out the following link. [Answer](http://stackoverflow.com/questions/13737485/java-lang-illegalstateexception-cant-parcel-a-recycled-bitmap-error-occurs) – Tulsi Jan 03 '17 at 05:57

1 Answers1

0

I think the issue is because serilization is done after the intent is fired. So instead of the bitmap, just put a copy of the Bitmap on the Intent, like this:

extras.putParcelable("imagebitmap",image.copy(image.getConfig(), true));
intent.putExtras(extras);

Check if this fixes the issue.

AI.
  • 934
  • 2
  • 14
  • 30
Sunil Sunny
  • 3,949
  • 4
  • 23
  • 53