将 File:///... 字符串转换为绝对 URI

Converting a File:///... string to an Absolute URI

所以我正在制作一个应用程序,让用户可以从他们的图库中选择图像进行显示,以及从相机中拍摄照片。

图像是通过采用文件路径字符串的位图预览的。当用户拍照时,下面的代码会处理它(并且有效):

String path = mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg"
File pictureFile = new File(path);
Bitmap bitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
            image1.setImageBitmap(bitmap);

路径格式如下:

/storage/emulated/0/Pictures/App/IMG_20150512_130719.jpg

然而,当我尝试从我的库中 select 时,我的代码已设置为我返回的字符串格式如下:

file:////storage/emulated/0/Pictures/App/IMG_20150512_130719.jpg

现在显然我可以(并且目前正在)拆分字符串并获得与上面匹配的路径,但我很好奇是否有标准的方法可以将它们转换为?

我看到 Uri.parse() 但这不起作用,因为那个 returns 一个 URI,我想要一个字符串,我可以从中创建一个文件,然后获取绝对路径。

如果您对我如何从 file://// 字符串移动到 / 字符串有明确的建议,我洗耳恭听。

谢谢

正在从文件 uri 转换为文件路径:

    String fileUri = "file:///storage/emulated/0/";
    String filePath = Uri.parse(fileUri).getPath();

正在从文件路径转换为文件 uri:

    String filePath2 = "/storage/emulated/0/";
    String fileUri2 = Uri.fromFile(new File(filePath2)).toString();