【问题标题】:Get Path File From Uri in android studio在 android studio 中从 Uri 获取路径文件
【发布时间】:2020-12-07 06:21:32
【问题描述】:

我无法从 Zip 文件类型或 APK 文件类型的 Uri 获取路径文件。在 onActivityResult 中

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

intent.setType("*/*");

startActivityForResult(intent, KeyGallery);

//获取路径

String picturePath = data.getData().getPath();

我收到 /document/415 :|

请帮帮我

【问题讨论】:

  • /document/415 有什么问题?你应该得到什么?
  • 不要尝试将 uri 转换为文件系统路径。使用 uri 本身。

标签: java android android-studio android-activity


【解决方案1】:

你可以使用这个函数来获取文件路径。

public static String getFilePath(Context context, Uri uri) {
        String imagePath;
        String[] filePath = {MediaStore.Images.Media.DATA};
        Cursor c = context.getContentResolver().query(uri, filePath, null, null, null);
        assert c != null;
        c.moveToFirst();
        int columnIndex = c.getColumnIndex(filePath[0]);
        imagePath = c.getString(columnIndex);
        c.close();
        return imagePath;
    }

希望这会有所帮助!快乐编码!

【讨论】:

    【解决方案2】:

    这对我有用!

    用途:

    String path = data.getData().getPath() // "/mnt/sdcard/FileName.mp3"
    File file = new File(new URI(path));
    

    String path = data.getData().toString() // "file:///mnt/sdcard/FileName.mp3"
    File file = new File(new URI(path));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多