pass uri to another activity and convert it to image

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) 

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

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

Tags:

Android