0

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

Mirko Marasco
  • 107
  • 2
  • 8
  • What do you mean by "if I print the value of the bitmap image in the secondary class it is present?" – cincy_anddeveloper Jul 09 '19 at 14:05
  • 1
    Possible duplicate of [Passing android Bitmap Data within activity using Intent in Android](https://stackoverflow.com/questions/11010386/passing-android-bitmap-data-within-activity-using-intent-in-android) – codegames Jul 09 '19 at 14:11

2 Answers2

2

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.

Froyo
  • 17,947
  • 8
  • 45
  • 73
  • I saved the bitmap image on the file and managed to pass it to the main activity, only they are black. So could you tell me how to capture the frame from the video view? @ – Mirko Marasco Jul 09 '19 at 14:21
  • @MirkoMarasco There are several questions on SO regarding this. https://stackoverflow.com/questions/27434087/how-to-capture-screenshot-or-video-frame-of-videoview-in-android https://stackoverflow.com/questions/19629160/how-to-capture-a-frame-from-video-in-android These might be helpful. It seems that you'd need to `MediaMetadataRetriever` – Froyo Jul 09 '19 at 21:56
0

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

Joker
  • 153
  • 14