Transferring an image from one activity to another action

There are similar questions on SO, but no one worked for me.

I want to get a clicked image in Activity1 and display it in Activity2.
I get the image with the id of the clicked image as follows:

((ImageView) v).getId() 

and passing it through the intention of another activity.

In the second step, I use the image identifier as follows:

 imageView.setImageResource(imgId); 

I registered the og image id value both in actions and in the same one.

But I get the following exception:

 android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x12/d=0x0 a=2 r=0x7f050000} 

I think the problem here is getId() returns an Id ImageView and not from the original image .
All these images are present in drawable .

Any help was appreciated.

+1
source share
4 answers

There are 3 solutions to this problem.

1) First convert the image to a byte array, and then go to Intent and in the next step get the byte array from Bundle and Convert into Image (Bitmap) and set it to ImageView.

Convert bitmap to byte array: -

 Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] byteArray = stream.toByteArray(); 

Pass an array of bytes to the target: -

 Intent intent = new Intent(this, NextActivity.class); intent.putExtra("picture", byteArray); startActivity(intent); 

Get byte array from Bundle and convert to bitmap: -

 Bundle extras = getIntent().getExtras(); byte[] byteArray = extras.getByteArray("picture"); Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); ImageView image = (ImageView) findViewById(R.id.imageView1); image.setImageBitmap(bmp); 

2) First save the image in the SDCard and in the next step set this image to ImageView.

3) Pass Bitmap to Intent and get a bitmap in the next activity from the package, but the problem is that the size of your Bitmap / Image is large at the time when the image does not load in the next step.

+20
source

This will not work. You should try it like this.

Set the DrawingCache of your ImageView to true, and then save the background as a bitmap and pass it through putExtra.

 image.setDrawingCacheEnabled(true); Bitmap b=image.getDrawingCache(); Intent i = new Intent(this, nextActivity.class); i.putExtra("Bitmap", b); startActivity(i); 

And in the next exercise

 Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Bitmap"); imageView.setImageBitmap(bitmap); 
+5
source

Define the Drawable static variable in your Application class, and then specify the data to draw the image in exercise one, and then in your next exercise get the data from the static variable that you ignored in your Application class.

 public class G extends Application { public static Drawable imageDrawable; ... } 

First lesson:

 G.imageDrawable = imageView.getDrawable(); 

SecondActivity:

 imgCamera.setImageDrawable(G.imageDrawable); 

and in the undestroy:

 @Override protected void onDestroy() { G.imageDrawable = null; super.onDestroy(); } 

Note. You must define the Application class in the manifest:

 <application android:name=".G" ...> </application> 
0
source

The perfect way to keep it short. This is the sender code .class file

 Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher; Intent intent = new Intent(); Intent.setClass(<Sender_Activity>.this, <Receiver_Activity.class); Intent.putExtra("Bitmap", bitmap); startActivity(intent); 

and this is the recipient class file code.

 Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("Bitmap"); ImageView viewBitmap = (ImageView)findViewById(R.id.bitmapview); viewBitmap.setImageBitmap(bitmap); 

No need to compress. what he

-1
source

All Articles