如何在android中获取前缀为file://的文件路径?

时间:2018-01-30 01:01:59

标签: android file filepath

这会给我一个这样的文件路径: 的 /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 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:1)

Go like this:

ERRORLEVEL

Picasso也可以接受文件。所以在你的代码中你就完成了这一点:

face_crop = []
for f in faces:
    x, y, w, h = [ v for v in f ]
    cv2.rectangle(image_copy, (x,y), (x+w, y+h), (255,0,0), 3)
    # Define the region of interest in the image  
    face_crop.append(gray_image[y:y+h, x:x+w])

for face in face_crop:
    cv2.imshow('face',face)
    cv2.waitKey(0)

你需要把它放在毕加索

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

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

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