简体   繁体   中英

Sending bitmap image from one activity to another

I would have a question to ask you about a problem I am having regarding the passage of a Bitmap image from an activity secondary to the main Activity. In the secondary activity I have a videoView, I have set up a button that, when pressed, extracts a frame from the videoView. The code I used to extract the frame in bitmap format is as follows:

 videoField.setDrawingCacheEnabled(true);
            videoField.buildDrawingCache();
            Bitmap bm = videoField.getDrawingCache();
            System.out.println(bm);
            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtra("BitmapImage", bm);
            startActivity(intent);

After doing this in the onCreate () of the main Activity, get the bitmap image as follows:

Intent intent = getIntent();
    Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

The problem is that when I get the bitmap image, the variable is always null, and i cant't set it on imageView. I can't understand the reason because if I print the value of the bitmap image in the secondary class it is present. Can anybody help me.

Thanks in advance

There are 2 ways to send Bitmap from one activity to another.

ByteArray.

Create a byteArray of the bitmap and send it via Intent.

ByteArrayOutputStream bStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream);
byte[] byteArray = bStream.toByteArray();

Intent anotherIntent = new Intent(this, anotherActivity.class);
anotherIntent.putExtra("image", byteArray);
startActivity(anotherIntent);

In your other activity,

Bitmap bmp;

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

Note: This method is not ideal as there's a limit of 1MB of data that you can pass in the Intent. So if the data is more than 1MB, it will crash.

There's a safer way.

Uri/File

1 Save the bitmap as an image in your app's cache directory. This would give you a Uri of the file. Pass this Uri via Intent.

val file = File(context.filesDir, name)
context.openFileOutput(file.name, Context.MODE_PRIVATE).use {
    it.write(bStream.toByteArray())
}

Now, you can pass the name via Intent.

2 In your next activity, obtain the Uri from Intent and load the Bitmap.

val file = File(context.filesDir, name)
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(file, options);

This is a safer way to pass Bitmap from one activity to another.

Solution 1 Convert it to a Byte array and pass with intent

Solution 2 Store bitmap in memory then pass the path of file in intent and access this file in next activity

Secound Solution in best because sending byte array some time causes OutOfMemory Problem when we have large bitmap

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM