There are similar questions on SO, but none worked for me.
I want to fetch clicked image in Activity1 and display it in Activity2.
I'm fetching image id of clicked image like this:
((ImageView) v).getId()
and passing it through intent to another activity.
In 2nd activity, I use image id as following:
imageView.setImageResource(imgId);
I logged the value og image id in both the activities and it's same.
But I'm getting 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 guess the problem here is getId()
is returning Id of ImageView
and not of it's source image.
All these images are present in drawable
.
Any help appreciated.
This example demonstrates how to pass an image from one activity to another activity in Android using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Using Intents This example demonstrate about How to send data from one activity to another in Android using intent. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
putExtra() : This method sends the data to another activity and in parameter, we have to pass key-value pair. Add the below code in onClick() method. intent. putExtra("full_name", fullName);
There are 3 Solutions to solve this issue.
1) First Convert Image into Byte Array and then pass into Intent and in next activity get byte array from Bundle and Convert into Image(Bitmap) and set into 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 byte array into intent:-
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);
Get Byte Array from Bundle and Convert into Bitmap Image:-
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 image into SDCard and in next activity set this image into ImageView.
3) Pass Bitmap into Intent and get bitmap in next activity from bundle, but the problem is if your Bitmap/Image size is big at that time the image is not load in next activity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With