pass uri to another activity and convert it to ima

2020-07-10 08:28发布

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

标签: android
5条回答
倾城 Initia
2楼-- · 2020-07-10 09:06

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();
}
查看更多
虎瘦雄心在
3楼-- · 2020-07-10 09:07

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

查看更多
ら.Afraid
4楼-- · 2020-07-10 09:18

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) 
查看更多
手持菜刀,她持情操
5楼-- · 2020-07-10 09:18

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

            }
查看更多
孤傲高冷的网名
6楼-- · 2020-07-10 09:22
  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);
    
查看更多
登录 后发表回答