从 android.net.Uri
对象(包含 file:
类型)转换为 Android 中的 File
对象的最简单方法是什么?
我尝试了以下方法,但它不起作用:
File file = new File(Environment.getExternalStorageDirectory(), "read.me");
Uri uri = Uri.fromFile(file);
File auxFile = new File(uri.toString());
assertEquals(file.getAbsolutePath(), auxFile.getAbsolutePath());
原文由 hpique 发布,翻译遵循 CC BY-SA 4.0 许可协议
你想要的是…
… 并不是…
笔记
android.net.Uri
object which is nameduri
and created exactly as in the question ,uri.toString()
returns aString
in the format"file:///mnt/sdcard/myPicture.jpg"
,而uri.getPath()
返回String
格式为"/mnt/sdcard/myPicture.jpg"
。