hmkcode / Android

Android related examples
3.52k stars 3.41k forks source link

Not a document Error #4

Open andyedward opened 9 years ago

andyedward commented 9 years ago

Hi,

Thanks for the util. Helped me a lot. But I encounter one issue when i try to get the URI from camera intent.

Phone is Sony Experia Z, android 4.4, the camera stores the images in an external SD Card The URI returned will be something like content://media/external/images/media/174394

Because it is 4.4, it will go to getRealPathFromURI_API19() method, but it will fail at this line String wholeID = DocumentsContract.getDocumentId(uri); returning a Not a document error

So what I did (which is working for me) was

 public static String getRealPathFromURI_API19(Context context, Uri uri){
    String filePath = "";

    if (String.valueOf(uri).contains("documents")) {

        String wholeID = DocumentsContract.getDocumentId(uri);

        // Split at colon, use second item in the array
        String id = wholeID.split(":")[1];

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

        // where id is equal to
        String sel = MediaStore.Images.Media._ID + "=?";

        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                column, sel, new String[]{ id }, null);

        int columnIndex = cursor.getColumnIndex(column[0]);

        if (cursor.moveToFirst()) {
            filePath = cursor.getString(columnIndex);
        }
        cursor.close();

    } else {
       filePath =  getRealPathFromURI_API11to18(context, uri);

    }
    return filePath;
}

But, do you think it is a good idea? or there is a better condition to check rather than checking if uri contains the word 'documents'?

lakshmikantdeshpande commented 9 years ago

I think your way is great....

HolenZhou commented 8 years ago

great!