首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Android中将file: Uri转换为File

在Android中将file: Uri转换为File
EN

Stack Overflow用户
提问于 2010-06-04 22:48:06
回答 23查看 497.1K关注 0票数 470

在安卓系统中,从file: android.net.Uri转换为File最简单的方法是什么?

尝试了以下方法,但不起作用:

代码语言:javascript
复制
 final File file = new File(Environment.getExternalStorageDirectory(), "read.me");
 Uri uri = Uri.fromFile(file);
 File auxFile = new File(uri.toString());
 assertEquals(file.getAbsolutePath(), auxFile.getAbsolutePath());
EN

回答 23

Stack Overflow用户

回答已采纳

发布于 2011-12-04 03:23:22

你想要的是。

代码语言:javascript
复制
new File(uri.getPath());

..。而不是..。

代码语言:javascript
复制
new File(uri.toString());

注意: uri.toString()返回格式为:"file:///mnt/sdcard/myPicture.jpg"的字符串,而uri.getPath()返回格式为:"/mnt/sdcard/myPicture.jpg"的字符串。

票数 883
EN

Stack Overflow用户

发布于 2016-07-25 21:08:30

使用

代码语言:javascript
复制
InputStream inputStream = getContentResolver().openInputStream(uri);    

然后直接复制文件。另请参阅:

https://developer.android.com/guide/topics/providers/document-provider.html

票数 71
EN

Stack Overflow用户

发布于 2013-05-13 03:17:23

在寻找了很长一段时间后,这对我来说是有效的:

代码语言:javascript
复制
File file = new File(getPath(uri));


public String getPath(Uri uri) 
    {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
        if (cursor == null) return null;
        int column_index =             cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String s=cursor.getString(column_index);
        cursor.close();
        return s;
    }
票数 61
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2975197

复制
相关文章

相似问题

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