18

I encounter a problem by picking images from gallery with android 5.0. My code for starting intent is:

private void takePictureFromGallery() 
{
    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(intent, PICK_FROM_FILE);
}

and here is function called in onActivityResult() method for request code PICK_FROM_FILE

private void handleGalleryResult(Intent 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]);
    // field declaration private String mTmpGalleryPicturePath;
    mTmpGalleryPicturePath = cursor.getString(columnIndex);
    cursor.close();
    // at this point mTmpGalleryPicturePath is null
    ...
}

For previous versions than 5.0 this code always work, using com.android.gallery application. Google Photos is default gallery application on Android 5.0. Could be this problem depends by application or is an issue of new android OS distribution?

EDIT

I understand the problem: Google Photos automatically browse content of its backupped images on cloud server. In fact trying pratice suggest by @maveň if i turn off each internet connections and after choose an image, it doesn't get result by decoding Bitmap from InputStream.

So at this point question become: is there a way in android 5.0 to handle the Intent.ACTION_PICK action so that system browse choose in local device image gallery?

and.ryx
  • 763
  • 1
  • 7
  • 16
  • I'm sorry but, still don't understand... getPath() method that you call in onResult() is method written in [link](http://stackoverflow.com/questions/20514973/android-kitkat-get-pick-an-image-from-androids-built-in-gallery-app-program/20516110#20516110) (in this case you've wrong arguments syntax) or is a function written by yourself? Anyway i try to get the path as is shown in the link without decode stream with BitmapFactory but doesn't work. – and.ryx Nov 20 '14 at 14:12

2 Answers2

36

I found solution to this problem combining following methods. Here to start activity for pick an image from gallery of device:

private void takePictureFromGallery() 
{
    startActivityForResult(
        Intent.createChooser(
            new Intent(Intent.ACTION_GET_CONTENT)
            .setType("image/*"), "Choose an image"), 
        PICK_FROM_FILE);
}

Here to handle result of intent, as described in this post, note that getPath() function works differently since android build version:

private void handleGalleryResult(Intent data) 
{
    Uri selectedImage = data.getData();
    mTmpGalleryPicturePath = getPath(selectedImage);
    if(mTmpGalleryPicturePath!=null)
        ImageUtils.setPictureOnScreen(mTmpGalleryPicturePath, mImageView);
    else
    {
        try {
            InputStream is = getContentResolver().openInputStream(selectedImage);
            mImageView.setImageBitmap(BitmapFactory.decodeStream(is));
            mTmpGalleryPicturePath = selectedImage.getPath();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

@SuppressLint("NewApi")
private String getPath(Uri uri) {
    if( uri == null ) {
        return null;
    }

    String[] projection = { MediaStore.Images.Media.DATA };

    Cursor cursor;
    if(Build.VERSION.SDK_INT >19)
    {
        // Will return "image:x*"
        String wholeID = DocumentsContract.getDocumentId(uri);
        // Split at colon, use second item in the array
        String id = wholeID.split(":")[1];
        // where id is equal to             
        String sel = MediaStore.Images.Media._ID + "=?";

        cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
                                      projection, sel, new String[]{ id }, null);
    }
    else
    {
        cursor = getContentResolver().query(uri, projection, null, null, null);
    }
    String path = null;
    try
    {
        int column_index = cursor
        .getColumnIndex(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        path = cursor.getString(column_index).toString();
        cursor.close();
    }
    catch(NullPointerException e) {

    }
    return path;
}
  • takePictureFromGallery() is invoked from onActivityResult

Thats all!!

Community
  • 1
  • 1
and.ryx
  • 763
  • 1
  • 7
  • 16
  • 1
    I don't think that is correct. It should be if(Build.VERSION.SDK_INT > 19) instead of if(Build.VERSION.SDK_INT >=19) – Юрій Мазуревич Feb 07 '15 at 08:25
  • 3
    I am getting IllegalArgumentException: Invalid URI: content://com.google.android.apps.photos.contentprovider/0/1/mediaKey%3A...6mkQk-P4tzU/ACTUAL/11...80 for Photos that are stored online within Google Photos. – lschmierer Aug 18 '15 at 17:16
  • Nice solution work for me in Andorid 5.0, pciking images from other folders. – Pankaj Aug 25 '15 at 07:04
6

Try this:

//5.0
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, CHOOSE_IMAGE_REQUEST);

Use the following in the onActivityResult:

Uri selectedImageURI = data.getData();
input = c.getContentResolver().openInputStream(selectedImageURI);
BitmapFactory.decodeStream(input , null, opts);

Update

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

Uri selectedImageUri = data.getData();  
String tempPath = getPath(selectedImageUri);

/**
* helper to retrieve the path of an image URI
*/
public String getPath(Uri uri) {
    if( uri == null ) {
        return null;
    }
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
    if( cursor != null ){
        int column_index = cursor
        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    return uri.getPath();
  }

  }  }

tempPath will store the path of the ImageSelected

Check this for more detail

Community
  • 1
  • 1
Maveňツ
  • 9,147
  • 14
  • 53
  • 94
  • Ok it work but how can i retrieve string path of photo? I need for it!! – and.ryx Nov 20 '14 at 11:53
  • @and.ryx what do you mean by `string path of photo` ? – Maveňツ Nov 24 '14 at 08:41
  • if, for example, file "picture01.jpg" is storing in "Photos" folder of "sdcard" directory my String output should be: "/sdcard/Photos/picture01.jpg" so the path of image file... – and.ryx Nov 24 '14 at 11:59
  • Path is not fixed it changes from device to device. like in some devices it would be `/sdcard/Photos/picture01.jpg` and in others `/sdcard0/Photos/picture01.jpg` – Maveňツ Nov 24 '14 at 12:01
  • @and.ryx debug and check the value of the `String` ....The code I have given works fine for me, test this code on device. – Maveňツ Nov 25 '14 at 13:52
  • In what android version did you try it? – and.ryx Nov 25 '14 at 14:07
  • On Both 4.4 and 5.0 API levels – Maveňツ Nov 25 '14 at 14:10
  • Very strange...on android 5.0 doesn't work because Intent is resolved By Google Photos application that browse me into cloud backup content. After when i try to get path of photo it is null. uri.getPath() method to retrieve path isn't a good solution, because i need for physical file path to retrieve the picture's file. – and.ryx Nov 25 '14 at 14:20