8

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 ));
Moudiz
  • 7,211
  • 22
  • 78
  • 156
  • Any errors you are facing ?? – Satyen Udeshi Sep 19 '15 at 05:14
  • @SatyenUdeshi java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.sqlfirst.AddImage}: java.lang.NullPointerException: uriString .. this error is for uri.parse – Moudiz Sep 19 '15 at 05:16
  • ok then in your `onActivityResult()` method check if `data.getData()` is `null` or is returning `URI` and yes can you post your `log cat stacktrace` – Satyen Udeshi Sep 19 '15 at 05:18
  • @SatyenUdeshi but it is returning dataI placed a systemoutputln , do you another way to get the intent ? I read in another question is to get it through inputStream = getContentResolver().openInputStream(selectedImage); but I didnt know how to do it – Moudiz Sep 19 '15 at 05:23
  • did you try coverting your `Uri` to `String`, try this http://stackoverflow.com/questions/8017374/how-to-pass-a-uri-to-an-intent – Satyen Udeshi Sep 19 '15 at 05:26
  • @SatyenUdeshi I have a question please .. how to get the uri like this To open the content for a Uri, use openInputStream() on a ContentResolver – Moudiz Sep 19 '15 at 05:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90088/discussion-between-satyen-udeshi-and-moudiz). – Satyen Udeshi Sep 19 '15 at 05:40

5 Answers5

10

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) 
Satyen Udeshi
  • 3,223
  • 1
  • 19
  • 26
4
  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);
    
Sunil
  • 3,785
  • 1
  • 32
  • 43
2

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

Vaibhav Barad
  • 625
  • 8
  • 17
  • I have a question please .. how to get the uri like this To open the content for a Uri, use openInputStream() on a ContentResolver – Moudiz Sep 19 '15 at 05:38
  • calling `getContentResolver().openInputStream(uri)` will only get you an InputStream from a URI. – Vaibhav Barad Sep 19 '15 at 05:47
  • yes and if i did that can i convert the inmput stream into an image with BitmapFactory ? if yes can you show me an example pleasee ? – Moudiz Sep 19 '15 at 05:51
  • I tried your code but I have this error .. how to solve it The method openInputStream(Uri) in the type ContentResolver is not applicable for the arguments (InputStream) – Moudiz Sep 19 '15 at 08:38
  • Try InputStream in = context.getContentResolver().openInputStream(Uri.parse(imagePath)); – Vaibhav Barad Sep 19 '15 at 09:29
1

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

            }
  • I got this error The method exists() is undefined for the type String – Moudiz Sep 19 '15 at 05:33
  • in next activity first get the URI from previous and store it into a string. declare a file and assign it . The use BitmapFactory then set imageView object to bitmap – android_softy Sep 19 '15 at 05:36
  • I am getting nullexception on this File imgFile = new File(image_path); .. is it possible in the main I am not getting intent? – Moudiz Sep 19 '15 at 05:42
  • means when your are sending ImagePath from previous activity your path is not coming – android_softy Sep 19 '15 at 05:43
  • first check your code are you properly getting ImagePath or not.. thats why you are getting Nullpointer Exception – android_softy Sep 19 '15 at 05:43
  • but why I am getting the path ? .. everything is normal in the app , clicking the button and choosing the image . please check is my code correct ? – Moudiz Sep 19 '15 at 05:45
  • you are clicking picture are getting image from gallery ? – android_softy Sep 19 '15 at 05:49
  • please join the chat above that i have started with satyen .. i am waiting for you – Moudiz Sep 19 '15 at 05:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90089/discussion-between-android-softy-and-moudiz). – android_softy Sep 19 '15 at 05:54
0

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();
}
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393