如何在安卓系统中获取以file://为前缀的文件路径?

4
这将给我一个文件路径,例如:/storage/emulated/0/myimage.jpg
File imageFile = File(Environment.getExternalStorageDirectory(),   "myimage.jpg");
String path = destination.getAbsolutePath();
Log.d("path", path);

如果我使用这个路径将图片用Picasso加载到ImageView中,它不会起作用。
Picasso.with(this.context).load("/storage/emulated/0/myimage.jpg").into(imageView);

只有在路径前加上file://才能正常工作。

Picasso.with(this.context).load("file:///storage/emulated/0/myimage.jpg").into(imageView);

有没有一种方法可以使用file://获取文件路径,而不是手动获得绝对路径并在前面添加?

2个回答

3

您需要使用Uri.fromFile来从文件获取URI

Uri uriFileName = Uri.fromFile(yourFile);
String uriStr = uriFileName.toString();

示例:"file:///tmp/android.txt"


3

像这样进行Go语言编程:

String path = "/storage/emulated/0/";

File file = new File(path , "myImage.jpg");

Picasso.with(mContext).load(file).into(ImageView);

毕加索也可以接受文件。因此在您的代码中,您已经完成了以下操作:
File imageFile = File(Environment.getExternalStorageDirectory(),   "myimage.jpg");

你只需要将它放入Picasso中

Picasso.with(mContext).load(imageFile).into(ImageView);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接