Intent.ACTION_GET_CONTENT opens recent files which gives a bad URI
Asked Answered
P

1

9

I'm using Intent.ACTION_GET_CONTENT which opens recent files. Selecting items from the recent files gives a bad URI but selecting the same file from the file manager gives a right URI which can be handled by my code.

public static String getRealPathFromURI(Context context, Uri uri) {
    String path;
    if ("content".equals(uri.getScheme())) {
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        path = cursor.getString(idx);
        cursor.close();
    } else {
        path = uri.getPath();
    }

    return path;
}

Note: The uri.getPath() output when I select a PDF from the recent files is /document/... but selecting the same file from the file manager is, .../emulated/....

Note: the error while selecting the file from the recent files is

Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

Psychologism answered 24/8, 2015 at 8:3 Comment(0)
P
3

The problem was my code doesn't handle the new Layout Storage URIs of Android. If you face this problem too, please refer to this link because the writer is wrote a fantastic method to get real path of every URIs.

Psychologism answered 24/8, 2015 at 10:42 Comment(1)
Thanks a lot !. Your linked article helped me to solve the problem.Oldenburg

© 2022 - 2024 — McMap. All rights reserved.