Java tutorial
//package com.java2s; //License from project: Apache License import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.MediaStore; import java.io.File; public class Main { /** * Get {@link File} object for the given Android URI.<br> * Use content resolver to get real path if direct path doesn't return valid file. */ private static File getFileFromUri(Context context, Uri uri) { // first try by direct path File file = new File(uri.getPath()); if (file.exists()) { return file; } // try reading real path from content resolver (gallery images) Cursor cursor = null; try { String[] proj = { MediaStore.Images.Media.DATA }; cursor = context.getContentResolver().query(uri, proj, null, null, null); if (cursor != null) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String realPath = cursor.getString(column_index); file = new File(realPath); } } catch (Exception ignored) { } finally { if (cursor != null) { cursor.close(); } } return file; } }