and.ryx and.ryx - 19 days ago 6
Android Question

Pick photo from gallery in android 5.0

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?

Answer

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!!

Comments