Question

[Solved] Passing image from one activity another activity

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.

Enquirer: GAMA

||

Solution #1:

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.

Respondent: Dipak Keshariya

Solution #2:

This won’t work. You have to try it this way.

Set the DrawingCache of your ImageView to be true and then save the background as a Bitmap and pass it via putExtra.

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

i.putExtra("Bitmap", b);
startActivity(i);

And in your Next Activity,

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Bitmap");
imageView.setImageBitmap(bitmap);
Respondent: Andro Selva

Solution #3:

Define a Drawable static variable in your Application class, and then set image drawable data in the first activity, then in your next activity get data from the static variable you defied in your Application class.

public class G extends Application {
   public static Drawable imageDrawable;

   ...
}

First activity:

G.imageDrawable = imageView.getDrawable();

SecondActivity:

imgCamera.setImageDrawable(G.imageDrawable);

and in onDestroy:

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

Note: You have to define your Application class in the manifest:

<application
        android:name=".G"
        ...>

</application>
Respondent: Nobody8

Solution #4:

If you are moving from class like addapter class then use this code.

Bitmap bitImg=listItem.getBitmapImage();
    ByteArrayOutputStream baoS = new ByteArrayOutputStream();
    bitImg.compress(Bitmap.CompressFormat.JPEG, 50, baoS);
    intent.putExtra("bitArray", baoS.toByteArray());
    context.getApplicationContext().startActivity(intent);

and then past this to next activity

if(getIntent().hasExtra("bitArray")) {                
Bitmap bitM = BitmapFactory.decodeByteArray( getIntent().getByteArrayExtra("bitArray"),0,getIntent().getByteArrayExtra("bitArray").length);       
        imgIT = findViewById(R.id.img_detail);
        imgIT.setImageBitmap(bitM);
    }
Respondent: Afzaal Ahmed

Solution #5:

The perfect way to do this in short.
This is the code of sender .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 receiver class file code.

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

No need to compress.
that’s it

Respondent: Abdul Qadir

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy