1

I am passing values from one activity to another activity through JSON. I am able to receive string values in the next activity. But I have no idea on sending the mage. Meanwhile all are dynamic values from array.

Sending like the following:

jsonobj.put(“Location”, “Chennai”);

jsonobj.put(“Name”, “test1”);

Receving like the following:

location = MainActivity.jsonobj.getString(“Location”);

name =MainActivity.jsonobj.getString(“Name”);

Please help me on how to send image dynamically and display it in the next activity.

Rahul Sharma
  • 2,867
  • 2
  • 27
  • 40

5 Answers5

1

You could pass the path of the image in intent as string and set it in imageview.

Intent intent = new Intent(getBaseContext(), ActivityB.class);
intent.putExtra("image_url", imageUrl);
startActivity(intent)
Sankar ganesh
  • 225
  • 1
  • 4
  • 12
0

In your first Activity

Convert ImageView to Bitmap

 imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);

In second Activity

  Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

Then set the bitmap to imageview.

Nidhin Kumar
  • 3,278
  • 9
  • 40
  • 72
0

Method 1:

In your first Activity

Convert ImageView to Bitmap

imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);

In second Activity

 Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

Then display bitmap in ImageView.

Method 2:

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);
Sjd
  • 1,261
  • 1
  • 12
  • 11
0

Simply pass it as a String and take it from the next activity.You said you can do that so i am not explaining that part!If you have a url anyway it's a string so no need to worry at all. Assuming you send the image name without its file type that's the light weight way.

If you want to get the ID of the image which is in the drawable folder use the String you got

int id1 = getResources().getIdentifier(StringImageNameWithOutPng, "drawable", getPackageName());

If you want the drawable

Drawable drawable = ContextCompat.getDrawable(YourCurrantActivity.this, id1);

Now use it as you want

whateverImageView.setImageDrawable(drawable);

More : https://stackoverflow.com/a/11519855/5188159

Community
  • 1
  • 1
Charuක
  • 12,953
  • 5
  • 50
  • 88
0

A simple way of passing data between 2 activities is as below.

FromActivity.java

Intent i = new Intent(FromActivity.this, ToActivity.class);
i.putExtra("ImageDetails", imgDetails);
startActivity(i);
/* Note: imgDetails can be an object with image details of ImageDetails.java class (or) 
you can set as many properties separately using putExtra multiple times. 
If you are using a class, make it serializable.
*/

ToActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    this.imgDetails = (ImageDetails) getIntent().getSerializableExtra("ImageDetails");
    ...
}

Thanks Sriram

Sri
  • 1,317
  • 9
  • 13