1

I used @AlexanderZaldostanov code and it worked perfectly

However i have a question on a part of the code that seems irrelevant and eventually not in use.

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        ImageView imageView = (ImageView) findViewById(R.id.imgView);

        Bitmap bmp = null;
        try {
            bmp = getBitmapFromUri(selectedImage);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        imageView.setImageBitmap(bmp);

    }


}

from the start of the String[] filePathColumn = { MediaStore.Images.Media.DATA }; line and all the way to the cursor.close(); At the end the Bitmap gets the Uri and thats it.

Why do we need all of the rest of the code in the middle?

Thanks,

Dima Kozhevin
  • 3,602
  • 9
  • 39
  • 52
SHAI86
  • 77
  • 1
  • 7

4 Answers4

0

You will not need the code in the middle if you just want to get a Bitmap from the Uri.

If you want the path of the image, eg. to upload the file to remote server, then that code is used to obtain the path of the image. But you shouldn't rely on this method to obtain path of the image.

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
  • 2
    "then that code is used to obtain the path of the image" -- that code will only work if the `Uri` has `content` scheme and came from the `MediaStore`. Even then, the resulting path may not be usable. – CommonsWare Nov 02 '17 at 12:57
0

if you need to captured image in Imageview than you can simply display into Imageview using imageView.setImageURI(selectedImage)

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();

        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        imageView.setImageURI(selectedImage);         

    }    

}

and the path of image needed when you need to upload your image in Server then that code is used to get the path of the image.

it will get the original path from the Image URI.

Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
0

Because when you select an image from the gallery there are different possibilites you select same image from different section. Say you can select an image from folder A and also same image can be fetched from recents.

Ex:From gallery it may give you

content://media/external/images/media/123

and from recents it may give content://com.android.providers.media.documents/document/image:132

The Content URI returned can be different depending upon where the selection has been made from. Especially since Kitkat (API level 19) which introduces the Storage Access Framework (SAF) you get to see all your document storage providers while selecting a photo.

The piece of code there will return the direct file system path of the image file

Sharath kumar
  • 4,064
  • 1
  • 14
  • 20
0

Why do we need all of the rest of the code in the middle?

You don't.

And since that code is fairly poor, you should not be using it in the first place. A Uri is not a file, and it does not have to point to a file, whether that Uri is from the MediaStore or anywhere else.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491