【问题标题】:How to get the file path with the prefix file:// in android?如何在android中获取前缀为file://的文件路径?
【发布时间】:2018-01-30 01:01:59
【问题描述】:

这会给我一个像这样的文件路径: /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:// 获取文件路径,而不是获取绝对路径然后手动添加它?

【问题讨论】:

    标签: android file filepath


    【解决方案1】:

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

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

    示例:“file:///tmp/android.txt”

    【讨论】:

      【解决方案2】:

      像这样去:

      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.with(mContext).load(imageFile).into(ImageView);
      

      【讨论】:

        猜你喜欢
        • 2017-12-03
        • 2013-08-25
        • 2021-02-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-20
        相关资源
        最近更新 更多