首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从媒体存储的URI中获取文件名和路径

从媒体存储的URI中获取文件名和路径
EN

Stack Overflow用户
提问于 2010-08-04 07:46:44
回答 25查看 409.1K关注 0票数 426

我有一个从媒体存储图像选择返回的onActivityResult,我可以使用以下命令获取图像的URI:

代码语言:javascript
复制
Uri selectedImage = data.getData();

将其转换为字符串将得到以下结果:

代码语言:javascript
复制
content://media/external/images/media/47

或指向给定的路径:

代码语言:javascript
复制
/external/images/media/47

然而,我似乎找不到一种方法来将其转换为绝对路径,因为我想将图像加载到位图中,而不必将其复制到其他地方。我知道这可以使用URI和内容解析器来完成,但这似乎在重启手机时中断了,我猜MediaStore在重启之间不会保持相同的编号。

EN

回答 25

Stack Overflow用户

发布于 2012-05-12 22:47:49

只需对第一个答案进行简单更新:mActivity.managedQuery()现在已被弃用。我已经用新方法更新了代码。

代码语言:javascript
复制
private String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    CursorLoader loader = new CursorLoader(mContext, contentUri, proj, null, null, null);
    Cursor cursor = loader.loadInBackground();
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    String result = cursor.getString(column_index);
    cursor.close();
    return result;
}

android dev source

票数 131
EN

Stack Overflow用户

发布于 2016-04-19 17:28:55

对于奥利奥

代码语言:javascript
复制
Uri uri = data.getData(); 
File file = new File(uri.getPath());//create path from uri
final String[] split = file.getPath().split(":");//split the path.
filePath = split[1];//assign it to a string(your choice).

对于Oreo下面的所有版本,我都做了这个方法,它从uri获取实际路径。

代码语言:javascript
复制
 @SuppressLint("NewApi")
    public static String getFilePath(Context context, Uri uri) throws URISyntaxException {
        String selection = null;
        String[] selectionArgs = null;
        // Uri is different in versions after KITKAT (Android 4.4), we need to
        if (Build.VERSION.SDK_INT >= 19 && DocumentsContract.isDocumentUri(context.getApplicationContext(), uri)) {
            if (isExternalStorageDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            } else if (isDownloadsDocument(uri)) {
                final String id = DocumentsContract.getDocumentId(uri);
                uri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
            } else if (isMediaDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];
                if ("image".equals(type)) {
                    uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }
                selection = "_id=?";
                selectionArgs = new String[]{
                        split[1]
                };
            }
        }
        if ("content".equalsIgnoreCase(uri.getScheme())) {


          if (isGooglePhotosUri(uri)) {
              return uri.getLastPathSegment();
           }

            String[] projection = {
                    MediaStore.Images.Media.DATA
            };
            Cursor cursor = null;
            try {
                cursor = context.getContentResolver()
                        .query(uri, projection, selection, selectionArgs, null);
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                if (cursor.moveToFirst()) {
                    return cursor.getString(column_index);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }
        return null;
    }

    public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    public static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    public static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }

  public static boolean isGooglePhotosUri(Uri uri) {
    return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
票数 115
EN

Stack Overflow用户

发布于 2011-09-01 09:20:52

不要试图在文件系统中查找uri,在数据库中查找会很慢。

您可以通过向工厂提供输入流来从uri获取位图,就像向工厂提供文件一样:

代码语言:javascript
复制
InputStream is = getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(is);
is.close();
票数 108
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3401579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档