1

How do I get an image from one activity to another using ImageView? Here is the code I tried:

img1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getBaseContext(),TrialVersion.class);
        intent.putExtra("design1",R.drawable.design1);
        startActivity(intent);
        finish();
    }
});
Charuක
  • 12,953
  • 5
  • 50
  • 88
paru
  • 11
  • 1
  • 3

3 Answers3

2

Solution 1:

Convert your Drawable to Bitmap and send it to Another Activity.

Bitmap bitmap = ((BitmapDrawable)d).getBitmap();

To send,

intent.putExtra("Bitmap", bitmap);

To Fetch,

Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");

Solution 2: (for drawable easy & light way)

Send the resource integer value like:

MAIN ACTIVITY

Intent intent = new Intent(this, SecondActivity.class); 
intent.putExtra("resourseInt", R.drawable.image);    
startActivity(intent); 

SECOND ACTIVITY

@Override 
public void onCreate(Bundle bundle) 
{
    super.onCreate(bundle); 
    setContentView(R.layout.yot_layout); 
    Bundle extras = getIntent().getExtras(); 
    if (extras == null) { 
        return; 
    } 
    int res = extras.getInt("resourseInt"); 
    ImageView view = (ImageView) findViewById(R.id.something); 
    view.setImageResourse(res); 
}
Saeid
  • 2,261
  • 4
  • 27
  • 59
0

No. This is not the Android way to deal with Image. You don't want to pass an Image to another activity by Intent. Intent is only suppose to be used to pass small and simple data.

You can save the image to disk and pass the path to another Activity. Or you can keep the image in an global object like Application, then access the image by this global object from another Activity.

The second way is risky because it may cause memory leak if you don't do it carefully and correctly.

Normally, image is loaded from local disk or network. In this case, you might want to consider some image library like Fresco and use the cache to load the image again from another Activity.

Simon J. Liu
  • 787
  • 4
  • 11
0

You can use the code like this:

Integer drawable = getIntent().getIntExtra("design1");
Ihor Dobrovolskyi
  • 1,241
  • 9
  • 19