其他分享
首页 > 其他分享> > Android Oreo如何从uri获得真正的路径?

Android Oreo如何从uri获得真正的路径?

作者:互联网

我正在使用此代码从URI获取真实路径.它正在努力
棉花糖但不在奥利奥:

private String getPathNew(Activity activity, Uri uri) { 
    if (isDownloadsDocument(uri)) {
        final String id = DocumentsContract.getDocumentId(uri);
        final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
        return getDataColumn(activity, contentUri, null, null);
    }
    return null;
}

public static String getDataColumn(Context context, Uri uri, String 
selection, String[] selectionArgs) {
    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = {column};
    try {
        cursor = context.getContentResolver().query(uri, projection, selection, 
selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

我究竟做错了什么?

解决方法:

 public static String getRealPathFromURI(Context context, Uri contentUri) {
    String[] proj = {MediaStore.Images.Media.DATA};
    Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, 
                                                                               null);
    if (cursor != null) {
        int column_index = 
        cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    return null;
}

标签:android,file-sharing,android-8-0-oreo,file-copying,android-8-1-oreo
来源: https://codeday.me/bug/20190710/1424902.html