Passing Image from One Activity 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.

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));
}

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

I want to transfer the image from one activity to another

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.

Note: this is not recommended. Should actually save the image somewhere and pass the path instead and retrieve from second activity.

How to send image from one activity to another in android?

I got the answer you need to send path of image from one activity to another.
filePath is the path of image.

Intent open_displayPage=new Intent(MainActivity.this,display_page.class);
open_displayPage.putExtra("imagePath", filePath);

And get the path in another activity

final String path = getIntent().getStringExtra("imagePath");
org_bmp = BitmapFactory.decodeFile(path);


Related Topics



Leave a reply



Submit