user3855628
user3855628

Reputation: 1

How can i select an image from gallery and display that in another activity..?

i have created the code to select image from gallery,but i can not pass that value to another activity through bundle..please help me

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            img.setImageURI(selectedImageUri);


enter code here

i need to pass the SelectedImageUri to another activity as bundle

Upvotes: 0

Views: 573

Answers (3)

J.R
J.R

Reputation: 2163

Use this

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                System.out.println("Image Path : " + selectedImagePath);
                img.setImageURI(selectedImageUri);
                Intent intent = new Intent(this , Second_activity.class );
                intent.putExtra("image_path", selectedImagePath);
                startActivity(intent);
}

it will start the Second Activity, then on the second Activity receive those values by this

 Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String value = extras.getString("image_path");
        //use value
    }

Upvotes: 1

Paresh Mayani
Paresh Mayani

Reputation: 128428

I need to pass the SelectedImageUri to another activity as bundle

=> FYI, Uri class itself implement Parcelable, so you can add and get value into/from Intent directly.

// Add a Uri instance to an Intent
intent.putExtra("SelectedImageUri", SelectedImageUri);

// Get a Uri from an Intent
Uri uri = intent.getParcelableExtra("SelectedImageUri");

Upvotes: 0

Alex Lungu
Alex Lungu

Reputation: 1114

I know is not exactly what you were looking for but if you pass selectedImagePath using i.putExtra("photoPath", selectedImagePath);, you can later load the image using only the path.

Upvotes: 0

Related Questions