Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 5.1.1 lollipop return null file path if image chosen from gallery

Tags:

android

Android 5.1.1 lollipop return null file path if image chosen from gallery. The below code works fine in all the devices below 5.1.1, but doesn't work in lollipop 5.1.1

Uri contentUri = data.getData();
Cursor cursor = null;
    try {
        String[] proj = {MediaStore.Images.Media.DATA};
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

cursor.getString(column_index) this returns null.

like image 993
Sanidhya Kumar Avatar asked Jun 01 '15 06:06

Sanidhya Kumar


People also ask

Why can't I get the photo file URI on Android 11?

The root of the issue seems to be that on Android 11 you must use FileProvider.getUriForFile () to generate the photo file URI that is passed to the camera app. Android-Image-Cropper uses Uri.fromFile ().

How to install Android 5 1 1 stock ROM?

Copy the downloaded Android 5.1.1 stock ROM zip file to the root folder on the phone's SD card and rename the folder as clockworkmod/backup Power off the phone and disconnect its USB cable from the computer

What GAPPS do I get with Android Lollipop?

The following Android 5.1 Lollipop Gapps includes: Google Play Store 5.4.12 These are minimal Gapps so you don’t get all the extra Google bloatware you don’t need, you can download any other Google apps using Play Store after installing!

How do I update my Android One to Lollipop?

Once restore process is complete, just reboot the device into the freshly installed Lollipop OS Your Android One device is now running the latest Android 5.1.1 Lollipop update via stock ROM. Go to Settings > About phone to verify the firmware version installed.


1 Answers

For now I have ended up with this for getting an image from gallery. I've tested it on 4.4, 5.0.1 and 5.1.1 but it should work on previous versions too (with new and old Google photo app), should be less hacky and doesn't require a check on Android version.

public static Uri handleImageUri(Uri uri) {
    if (uri.getPath().contains("content")) {
        Pattern pattern = Pattern.compile("(content://media/.*\\d)");
        Matcher matcher = pattern.matcher(uri.getPath());
        if (matcher.find())
            return Uri.parse(matcher.group(1));
        else
            throw new IllegalArgumentException("Cannot handle this URI");
    }
    return uri;
}

And with this I used the same code I have ever used before for getting the image path:

public static String getRealPathFromURI(Context context, Uri uri) {
    Cursor cursor = null;
    try {
        Uri newUri = handleImageUri(uri);
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(newUri,  proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } catch (Exception e){
        return null;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
like image 60
MatPag Avatar answered Nov 03 '22 01:11

MatPag