Moudiz
Moudiz

Reputation: 7377

pass uri to another activity and convert it to image

How to send a uri path of an image to another activity and convert it to image. I tried the below

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {

             super.onActivityResult(requestCode, resultCode, data);

              if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {

                  //file name
                     Uri selectedImage = data.getData();

                     Intent i = new Intent(this,
                              AddImage.class);
                    i.putExtra("imagePath", selectedImage);
                    startActivity(i);

and get it like this

 String imagePath = getIntent().getStringExtra("imagePath");
            imageview.setImageURI(Uri.parse(imagePath ));

Upvotes: 8

Views: 21226

Answers (5)

Satyen Udeshi
Satyen Udeshi

Reputation: 3243

Convert you URI to String while adding to Intent like given below

i.putExtra("imagePath", selectedImage.toString());

and in your NextActivity get the String and convert back to URI like ->

Intent intent = getIntent(); 
String image_path= intent.getStringExtra("imagePath"); 
Uri fileUri = Uri.parse(image_path) 
imageview.setImageURI(fileUri) 

Upvotes: 11

Suragch
Suragch

Reputation: 511666

To pass an image Uri to the next activity, you can just use setData() and getData(). There is no need to convert the Uri to anything.

First Activity

Intent intent = new Intent(this, SecondActivity.class);
intent.setData(uri);
startActivity(intent);

Second Activity

// get Uri
Uri uri = getIntent().getData();

// decode bitmap from Uri
if (uri == null) return;
try {
    InputStream stream = getContentResolver().openInputStream(uri);
    if (stream == null) return;
    Bitmap bitmap = BitmapFactory.decodeStream(stream);
    stream.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Upvotes: 0

Sunil
Sunil

Reputation: 3793

  1. First Activity

    Uri uri = data.getData();
    Intent intent=new Intent(Firstclass.class,secondclass.class);
    intent.putExtra("imageUri", uri.toString());
    startActivity(intent);
    
  2. Second class

    Imageview iv_photo=(ImageView)findViewById(R.id.iv_photo);
    Bundle extras = getIntent().getExtras();
    myUri = Uri.parse(extras.getString("imageUri"));
    iv_photo.setImageURI(myUri);
    

Upvotes: 4

Vaibhav Barad
Vaibhav Barad

Reputation: 625

to use the returned uir from the calling activity and then set it to a imageview you can do this

Uri imgUri=Uri.parse(imagePath);
imageView.setImageURI(null); 
imageView.setImageURI(imgUri);

This is a workaround for refreshing an ImageButton, which tries to cache the previous image Uri. Passing null effectively resets it.

For converting the inputStream into a bitmap you could do this

InputStream in = getContentResolver().openInputStream(Uri.parse(imagePath)); 
Bitmap bm = BitmapFactory.decodeStream(getContentResolver().openInputStream(in));

and then call

image.setImageBitmap(bm); 

to set it it a imageview, you could also check this link for an example

hope i could help

Upvotes: 2

android_softy
android_softy

Reputation: 139

in Next activity get that URI like this;

Intent intent = getIntent();
    String image_path= intent.getStringExtra("YOUR Image_URI");

and to convert that Image_URI to Image use Below mentioned Code

File imgFile = new File(image_path);
                if (imgFile.exists()) {

                Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                imageView.setImageBitmap(myBitmap);

            }

Upvotes: 1

Related Questions