如何在Android Lollipop中从URI获取图像的文件路径?

TOP*_*TOP 4 android uri filepath

我想为Android Lollipop创建一个图像选择器.我Intent.ACTION_GET_CONTENT用来选择我的图片但是我无法从URI中获取所选图像的路径,对话框选择器返回了该路径.

我试过这些主题的代码:

从gallery kitkat android中选择图像时检索绝对路径

https://chintankhetiya.wordpress.com/2013/12/14/picture-selection-from-camera-gallery/

但是所有这些都返回null.谁能帮我?

Art*_*tem 13

尝试

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.CursorLoader;
import android.database.Cursor;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.provider.MediaStore;

public class RealPathUtil {
    @SuppressLint("NewApi")
    public static String getRealPathFromURI_API19(Context context, Uri uri) {
        Log.variable("uri", uri.getPath());
        String filePath = "";
        if (DocumentsContract.isDocumentUri(context, uri)) {
            String wholeID = DocumentsContract.getDocumentId(uri);
            Log.variable("wholeID", wholeID);
// Split at colon, use second item in the array
            String[] splits = wholeID.split(":");
            if (splits.length == 2) {
                String id = splits[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 = uri.getPath();
        }
        return filePath;
    }

    @SuppressLint("NewApi")
    public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
        String[] proj = {MediaStore.Images.Media.DATA};
        String result = null;
        CursorLoader cursorLoader = new CursorLoader(
                context,
                contentUri, proj, null, null, null);
        Cursor cursor = cursorLoader.loadInBackground();
        if (cursor != null) {
            int column_index =
                    cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            result = cursor.getString(column_index);
        }
        return result;
    }

    public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) {
        String[] proj = {MediaStore.Images.Media.DATA};
        Cursor 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);
    }
Run Code Online (Sandbox Code Playgroud)

并使用:

private String uriToFilename(Uri uri) {
    String path = null;

    if (Build.VERSION.SDK_INT < 11) {
        path = RealPathUtil.getRealPathFromURI_BelowAPI11(mContext, uri);
    } else if (Build.VERSION.SDK_INT < 19) {
        path = RealPathUtil.getRealPathFromURI_API11to18(mContext, uri);
    } else {
        path = RealPathUtil.getRealPathFromURI_API19(mContext, uri);
    }

    return path;
}
Run Code Online (Sandbox Code Playgroud)

在必须的情况下适合我(不适用于下载应用程序)