0

I have both /document/video:5312 and content://com.android.providers.media.documents/document/video%3A5312

as "URI" or "absolute paths" to a video file that I selected from gallery and I feed it into new File();

File selectedFile = new File(selectedFilePath);

I've tried both of the upper URI/Paths and they just don't work. Does NOT work. The file is NOT found.

What format does File() require? This path is valid. How do I open it with File()?

David Kachlon
  • 599
  • 2
  • 5
  • 17

2 Answers2

2

"URI" or "absolute paths" to a video file that I selected from gallery

Those are Uri values. They are not filesystem paths, nor can you get filesystem paths for them.

The file is NOT found.

That is because they are not files. content://com.android.providers.media.documents/document/video%3A5312 has a scheme of content, not file.

Similarly, https://stackoverflow.com/questions/41900707/file-not-found-selecting-a-video-from-gallery is not a file, as that Uri has a scheme of https.

How do I open it with File()?

You don't, any more than you open https://stackoverflow.com/questions/41900707/file-not-found-selecting-a-video-from-gallery using File.

Instead, you use ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri, just as you might use OkHttp or HttpUrlConnection to get an InputStream on an https Uri.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Can you help me with an example of uploading a file with this method...? I only found one for Xamarin on google. – David Kachlon Jan 27 '17 at 18:46
  • @DavidKachlon: Well, few HTTP client APIs work purely off of an `InputStream`, in case they need to restart the HTTP operation. You would have to see if your chosen HTTP API supports `InputStream` (e.g., OkHttp does not). If it does, great. If not, also open a `FileOutputStream` somewhere (e.g., `getCacheDir()`), and copy the bytes from the `InputStream` to the `FileOutputStream`. Then, you can use your copy of the content as a file, deleting it when you are done. – CommonsWare Jan 27 '17 at 18:50
0

Ok. Thank you.

Here's what I managed....

    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;

    }

}

I get an EACES error upon trying to write my .mp4 file. Is there a way to "write it to memory" ? Or maybe you can resolve this issue.

David Kachlon
  • 599
  • 2
  • 5
  • 17