9
public static String getFilePathFromUri(Uri uri, Context c) {
    try {
        String filePath = null;
        String scheme = uri.getScheme();
        if (scheme != null && scheme.equals("content")) {
            ContentResolver contentResolver = c.getContentResolver();
            Cursor cursor = contentResolver.query(uri, null, null, null,
                    null);
            cursor.moveToFirst();
            filePath = cursor.getString(cursor
                    .getColumnIndexOrThrow(Images.Media.DATA));
        }
        return filePath;
    } catch (Exception e) {
        //java.lang.illegalArgumentException: column'_data' does not exist
        return null;
    }
}

I get this Exception When the Image is from Picasa folder.

4

1 回答 1

1
// try this way,hope this will help you...

public String getAbsolutePath(Uri uri) {
        if(Build.VERSION.SDK_INT >= 19){
            String id = uri.getLastPathSegment().split(":")[1];
            final String[] imageColumns = {MediaStore.Images.Media.DATA };
            final String imageOrderBy = null;
            Uri tempUri = getUri();
            Cursor imageCursor = getContentResolver().query(tempUri, imageColumns,
                    MediaStore.Images.Media._ID + "="+id, null, imageOrderBy);
            if (imageCursor.moveToFirst()) {
                return imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
            }else{
                return null;
            }
        }else{
            String[] projection = { MediaColumns.DATA };

            Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
            if (cursor != null) {
                int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            } else
                return null;
        }

}
于 2013-09-18T07:17:03.483 回答