Passing android Bitmap Data within activity using Intent in Android

前端 未结 7 1472
梦如初夏
梦如初夏 2020-11-22 09:36

I hava a Bitmap variable named bmp in Activity1 , and I want to send the bitmap to Activity2

Following is the code I use to pass it with the intent.

相关标签:
7条回答
  • 2020-11-22 10:08

    Convert it to a Byte array before you add it to the intent, send it out, and decode.

    //Convert to byte array
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    
    Intent in1 = new Intent(this, Activity2.class);
    in1.putExtra("image",byteArray);
    

    Then in Activity 2:

    byte[] byteArray = getIntent().getByteArrayExtra("image");
    Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    

    edit

    Thought I should update this with best practice:

    In your first activity, you should save the Bitmap to disk then load it up in the next activity. Make sure to recycle your bitmap in the first activity to prime it for garbage collection:

    Activity 1:

    try {
        //Write file
        String filename = "bitmap.png";
        FileOutputStream stream = this.openFileOutput(filename, Context.MODE_PRIVATE);
        bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    
        //Cleanup
        stream.close();
        bmp.recycle();
    
        //Pop intent
        Intent in1 = new Intent(this, Activity2.class);
        in1.putExtra("image", filename);
        startActivity(in1);
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    In Activity 2, load up the bitmap:

    Bitmap bmp = null;
    String filename = getIntent().getStringExtra("image");
    try {
        FileInputStream is = this.openFileInput(filename);
        bmp = BitmapFactory.decodeStream(is);
        is.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    Cheers!

    0 讨论(0)
提交回复
热议问题