【问题标题】:File not found... selecting a video from gallery找不到文件...从图库中选择视频
【发布时间】:2017-01-28 03:52:07
【问题描述】:

我都有 /文件/视频:5312 和 内容://com.android.providers.media.documents/document/video%3A5312

作为我从图库中选择的视频文件的“URI”或“绝对路径”并将其输入到 new File();

File selectedFile = new File(selectedFilePath);

我已经尝试了上面的两个 URI/路径,但它们都不起作用。 工作。 找不到文件。

File() 需要什么格式? 此路径有效。如何使用 File() 打开它?

【问题讨论】:

  • 你的问题太模糊了
  • 稍微修改了一下.. 好点了吗?
  • 我实际上没有得到你想要的?
  • stackoverflow.com/questions/3401579/… 可能会有所帮助或相关。在那里查看批准的答案

标签: android


【解决方案1】:

我从图库中选择的视频文件的“URI”或“绝对路径”

这些是Uri 值。它们不是文件系统路径,您也无法获取它们的文件系统路径。

找不到文件。

那是因为它们不是文件。 content://com.android.providers.media.documents/document/video%3A5312 的方案是 content,而不是 file

同样,https://stackoverflow.com/questions/41900707/file-not-found-selecting-a-video-from-gallery 不是文件,因为 Uri 的方案是 https

如何使用 File() 打开它?

你不会,就像你用File打开https://stackoverflow.com/questions/41900707/file-not-found-selecting-a-video-from-gallery一样。

取而代之,you use ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri,就像您可以使用 OkHttp 或 HttpUrlConnectionhttps Uri 上获得 InputStream

【讨论】:

  • 你能帮我举一个用这种方法上传文件的例子吗...?我在 google 上只找到了 Xamarin 的一个。
  • @DavidKachlon:嗯,很少有 HTTP 客户端 API 完全脱离 InputStream 工作,以防他们需要重新启动 HTTP 操作。您必须查看您选择的 HTTP API 是否支持InputStream(例如,OkHttp 不支持)。如果是这样,那就太好了。如果没有,还要在某处打开一个FileOutputStream(例如getCacheDir()),然后将字节从InputStream 复制到FileOutputStream。然后,您可以将内容副本用作文件,完成后将其删除。
【解决方案2】:

好的。谢谢。

这就是我所管理的......

    String[] perms = {"android.permission. WRITE_EXTERNAL_STORAGE"};

    int permsRequestCode = 200;

   requestPermissions(perms, permsRequestCode);



  public void convertFile(Uri urx)
        throws IOException {

    ContentResolver test = getContentResolver();
    InputStream initialStream =  test.openInputStream(urx);


    byte[] buffer = new byte[initialStream.available()];
    initialStream.read(buffer);

    File xpath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);


    File targetFile = new File(xpath, "/test.mp4");
    OutputStream outStream = new FileOutputStream(targetFile);
    outStream.write(buffer);
    uploadFile(targetFile.getPath());

}

@Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){

    switch(permsRequestCode){

        case 200:

            boolean writeAccepted = grantResults[0]== PackageManager.PERMISSION_GRANTED;

            break;

    }

}

我在尝试写入 .mp4 文件时收到 EACES 错误。有没有办法“将其写入内存”?或者你可以解决这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多