How to Pass Image Data from One Activity to Another Activity

Passing image from one activity another activity

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.

want to transfer the image from one activity to another

In your first MainActivity inside bitmap declare "public static" as below code:

class MainActivity extends AppCompatActivity implements View.OnClickListener {

Button choose;
public static Bitmap bitmap;

private int PICK_IMAGE_REQUEST = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

choose = (Button) findViewById(R.id.choose);
choose.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if (view == choose) {
showFileChooser();
}
}

private void showFileChooser() {

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();

try {
//Getting the Bitmap from Gallery
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
startActivity(new Intent(getApplicationContext(), SecondActivity.class));
}
catch (IOException e) {
e.printStackTrace();

}
}

}
}

And SecondActivity access code:

public class SecondActivity extends AppCompatActivity implements View.OnClickListener {

ImageView image;
Button upload;

Bitmap bitmap;

SharedPreferences sp;
String rollno;

private String UPLOAD_URL ="http://aptronnoida.com/applock/image_insert.php";

private String KEY_Rollno = "rollno";
private String KEY_IMAGE = "image";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
image=(ImageView)findViewById(R.id.image);

bitmap= MainActivity.bitmap ;
image.setImageBitmap(bitmap);
}
}

How to pass image data from one activity to another activity?

In MyGridView: (someInteger is an integer that represents the index of the selected image

Intent myIntent = new Intent(this, MyImageViewActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("image", someInteger);
myIntent.putExtras(bundle);
startActivityForResult(myIntent, 0);

In MyImageViewActivity:

Bundle bundle = this.getIntent().getExtras();
int pic = bundle.getInt("image");

of course, you can put anything in the bundle! maybe a byte array or something

Passing an image from an activity to another one

First the mipmap folders are for placing your app/launcher icons (which are shown on the homescreen) in only. Any other drawable assets you use should be placed in the relevant drawable folders.

Next since they will be in your drawables, I would just pass the @DrawableRes id e.g. the R.id.image_name value.

Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra(IMAGE_RES_ID_KEY, R.id.imageName);
startActivity(intent);

Also I would recommend that you use a public static variable IMAGE_RES_ID_KEY for your extra key to avoid typos.

Then on the other side you can simply

if(getIntent().hasExtra(MainActivity.IMAGE_RES_ID_KEY)) {
imageView.setImageResource(getIntent().getIntExtra(MainActivity.IMAGE_RES_ID_KEY, 0));
}

Passing of captured image or from gallery to another activity

Try this it is working fine for me.

 //Use  this method to select image from Gallery
private void processGalleryImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
GALLERY_REQUEST_CODE);
}

//Use this method to click image from Camera
private void processCameraImage() {
Intent cameraIntent = new
Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
}

//Use this method to get image from Gallery/Captured from Camera
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == CAMERA_REQUEST_CODE) {
Bitmap photo = (Bitmap) data.getExtras().get("data");

//Starting activity (ImageViewActivity in my code) to preview image
Intent intent = new Intent(this, ImageViewActivity.class);
intent.putExtra("BitmapImage", photo);
startActivity(intent);
} else if (requestCode == GALLERY_REQUEST_CODE) {
if (data.getData() != null) {
Uri imageUri = data.getData();

//Starting activity (ImageViewActivity in my code) to preview image
Intent intent = new Intent(this, ImageViewActivity.class);
intent.putExtra("ImageUri", imageUri.toString());
startActivity(intent);
}
}
}
}

//ImageViewActivity code

private Bitmap bitmap;
private String uri;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_view);
ButterKnife.bind(this);

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
bitmap = bundle.getParcelable("BitmapImage");
uri = bundle.getString("ImageUri");

if (bitmap != null)
imageView.setImageBitmap(bitmap);
else
Glide.with(this).load(uri).into(imageView);
}
}

I am using the Glide library to load image in image view in case if i have image url.

How to pass a image dynamically from one activity to another activity and display it in imageview through JSON

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



Related Topics



Leave a reply



Submit