0

I am trying to upload a video on server. This is how i get the video

intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
  intent.addCategory(Intent.CATEGORY_OPENABLE);
  intent.setType("video/*");
  startActivityForResult(intent, SELECT_VIDEO);

i get the file uri this way

Uri uri = data.getData();

this is what i recieve

content://com.android.providers.media.documents/document/video%3A165191

i show the video preview this way

mVideoView.setVideoURI(uri);
 mVideoView.requestFocus();
 mVideoView.start();

Preview plays perfectly. then i need to make a file to upload it to server . i make this file this way

file = new File(uri.getPath());

but when i reach the line below, it gives error

 FileInputStream fileInputStream = new FileInputStream(file);

i also tried uri.toString(); but still error. The error i recieve is below:

2 error: /document/video:165191: open failed: ENOENT (No such file or directory) java.io.FileNotFoundException: /document/video:165191: open failed: ENOENT (No such file or directory)

Please someone help me ... it is really making me frustrated ..

Faisal
  • 566
  • 5
  • 17

1 Answers1

0

The data URI that you get does not need to be a path on the file system - some apps implement it as a path, others as a unique URI which exposes their data through ContentResolver.

Your best bet would be to call openInputStream(uri), which returns an InputStream. Depending on your implementation of the server upload, you can either use that to stream bytes to the server directly, or pass the bytes to a FileOutputStream, save the data to a file and use that for your upload.

npace
  • 4,218
  • 1
  • 25
  • 35