Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing android Bitmap Data within activity using Intent in Android

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.

Intent in1 = new Intent(this, Activity2.class); in1.putExtra("image",bmp); startActivity(in1); 

And in Activity2 I try to access the bitmap using the following code

Bundle ex = getIntent().getExtras(); Bitmap bmp2 = ex.getParceable("image"); ImageView result = (ImageView)findViewById(R.Id.imageView1); result.setImageBitmap(bmp); 

The application runs without an exception but it does not give the expected result

like image 570
adi.zean Avatar asked Jun 13 '12 07:06

adi.zean


People also ask

How to send a bitmap from activity1 to activity2?

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. Intent in1 = new Intent (this, Activity2.class); in1.putExtra ("image",bmp); startActivity (in1); And in Activity2 I try to access the bitmap using the following code

How to pass the data through intent in Android?

To pass the data through Intent we will use putExtra () method and in parameter, we will use Key-Value Pair. Now, where we have to mention putExtra () method? We have to add putExtra () method in onClick () as shown in the below code and in parameter we have to mention key and its value.

How to avoid app crash from passing large bitmap data through intent?

We can also solve this without passing data through intent. Just store the image in the memory and in the next Activity just load the image from that location, which can also avoid app crash from passing large bitmap data. Note: You need not even pass the location path to the intent, remember the path and use it.

How to get the data from intent to main activity?

// Here we get the data using the geInt () method. From the second activity, pass the data to the Main activity by using setData () method, as I have already discussed in my previous article. //create an instance of the Intent object to return data.


2 Answers

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!

like image 182
Zaid Daghestani Avatar answered Oct 05 '22 23:10

Zaid Daghestani


Sometimes, the bitmap might be too large for encode&decode or pass as a byte array in the intent. This can cause either OOM or a bad UI experience.

I suggest to consider putting the bitmap into a static variable of the new activity (the one that uses it) which will carefully be null when you no longer need it (meaning in onDestroy but only if "isChangingConfigurations" returns false).

like image 34
android developer Avatar answered Oct 06 '22 01:10

android developer