找不到文件...从图库

时间:2017-01-27 18:31:33

标签: android

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

作为我从图库中选择的视频文件的“URI”或“绝对路径”,我将其提供给新的File();

File selectedFile = new File(selectedFilePath);

我已尝试过两个上层URI /路径,它们只是不起作用。 NOT 是否有效。该文件是 NOT 找到。

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

2 个答案:

答案 0 :(得分:2)

  

我从图库

中选择的视频文件的“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()打开它?

除了使用https://stackoverflow.com/questions/41900707/file-not-found-selecting-a-video-from-gallery打开File之外,您不会这样做。

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

答案 1 :(得分:0)

确定。谢谢。

这是我管理的......

    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错误。有没有办法将其写入内存" ?或者也许你可以解决这个问题。