转换文件:Uri到Android中的文件 [英] Convert file: Uri to File in Android
本文介绍了转换文件:Uri到Android中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从文件转换最简单的方法是:
android.net.Uri
至文件
在Android?
What's the easiest way to convert from a file:
android.net.Uri
to a File
in Android?
尝试以下但不起作用:
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());
推荐答案
你想要的是...
new File(uri.getPath());
...而不是...
... and not...
new File(uri.toString());
注意: url.toString() / code>返回一个格式为:
file:///mnt/sdcard/myPicture.jpg
,而 url的字符串。 getPath()
返回一个字符串格式:/ mnt / sdcard / myPicture.jpg
,
NOTE: url.toString()
return a String in the format: "file:///mnt/sdcard/myPicture.jpg"
, whereas url.getPath()
returns a String in the format: "/mnt/sdcard/myPicture.jpg"
,
这篇关于转换文件:Uri到Android中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文