0

I was able to pass image view to another layout but once I close the app or change the layout and go back to the layout with the passed image view. The image view disappears. My question is how do I keep the image view inside the layout it was passed too? Here's what I found online to pass image view.

FirstClass.java

RandomImageHere.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(getApplicationContext(), SecondClass.class);

            intent.putExtra("resourseInt", R.drawable.picture);
            startActivity(intent);

        }
    });

SecondClass.java

private ImageView imageView;

Bundle extras = getIntent().getExtras();
  imageView = (ImageView) findViewById(R.id.image_view);

    if (extras == null)
    {
        return;
    }
    int res = extras.getInt("resourseInt");
    imageView.setImageResource(res);

SecondClass.xml

<ImageView
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:id="@+id/image_view"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />
OBX
  • 6,044
  • 7
  • 33
  • 77
Sean
  • 179
  • 1
  • 2
  • 14

5 Answers5

1

you can save the image resource in SharedPreferences after extracting from Intent

PreferenceManager.getDefaultSharedPreferences(this).edit()
            .putInt("iv", res).commit();

then in onResume() method

int r = PreferenceManager.getDefaultSharedPreferences(this)
.getInt("iv", R.mipmap.ic_launcher);

imageView.setImageResource(r);
Majeed Khan
  • 505
  • 7
  • 16
1

You can save your resource id to sharedpreference:

private ImageView imageView;
SharedPreference savedImage;

In your OnCreate method:

OnCreate(){
.....savedImage = PreferenceManager.getDefaultSharedPreferences(this);
}

Then set image from preference if it contains:

Bundle extras = getIntent().getExtras();
  imageView = (ImageView) findViewById(R.id.image_view);

    if (extras == null)
    {
        return;
    }
    else{
        int res = extras.getInt("resourseInt");
        savedImage.edit().putInt("ImageID", res).apply();
        if(savedImage.contains("ImageID"){
            imageView.setImageResource(savedImage.getInt("ImageId", 0));
        }
    }
tahsinRupam
  • 6,325
  • 1
  • 18
  • 34
1

I was able to pass image view to another layout but once I close the app or change the layout and go back to the layout with the passed image view. The image view disappears.

You are adopting the wrong solution. If you are passing data from Activity FirstClass to -> SecondClass and require to access that data without the awareness of the FirstClass the next time, then you ought to save that specific info in storage. You can use SharedPreferences for this, this is how you do it:

In your FirstClass :

RandomImageHere.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        SharedPreferences pref = getSharedPreferences("Images", Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = pref.edit();
        ed.putInt("IMG", R.drawable.picture);
        ed.apply();
        Intent intent = new Intent(getApplicationContext(), SecondClass.class);
        startActivity(intent);

    }
});

Then in your SecondClass :

    private ImageView imageView;
    imageView = (ImageView) findViewById(R.id.image_view);
    SharedPreferences pref = getSharedPreferences("Images", Context.MODE_PRIVATE);
    int res = pref.getInt("IMG",0);
    if(res!=0)
    {
        imageView.setImageResource(res);

    }
OBX
  • 6,044
  • 7
  • 33
  • 77
1

Convert it to a Byte array before you add it to the intent, send it out, and decode.

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 other activity write below lines

byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);`enter code here`
Pratik
  • 452
  • 4
  • 17
0
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);     
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 
byte[] b = baos.toByteArray();

Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("picture", b);
startActivity(intent);

and Receiver Activity

Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("picture");

Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);
Dhara Patel
  • 359
  • 5
  • 19