4

I want to record a video, store it in internal storage and access the file.

FileProvider creates an URI for the camera intent to record a video:

File path = new File(this.getFilesDir(), "shared");
File file = new File(path, UUID.randomUUID().toString() + ".mp4");

// Store URI as property of activity
this.uri = FileProvider.getUriForFile(App.getInstance().getApplicationContext(), App.getInstance().getApplicationContext().getPackageName() + ".provider", file);

Intent videoCaptureIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
videoCaptureIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, durationLimit);
videoCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(videoCaptureIntent, REQUEST_VIDEO_CAPTURE);

The video file is stored in the internal storage of the app, I can see the file in Android File Explorer in the correct location.

However, I cannot access the file afterwards, the file cannot be found. The uri property still shows the correct uriString and authority.

if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_VIDEO_CAPTURE) {

    File file = new File(this.uri.getPath());
    Log.d(file.exists()); // Logs false
}

Why is that?

Manuel
  • 14,274
  • 6
  • 57
  • 130
  • 2
    `this.uri.getPath()` is not returning what you think it is. A URI path, broadly, is the section of segments that come after the authority (and optional port). It is not a file path. Why not just hang onto `File file`? – Mike M. Oct 18 '18 at 16:08
  • The app is currently set up to pass URIs around. Is there a way I get a `File` from the URI? – Manuel Oct 18 '18 at 16:13
  • btw what do you need `File` object for? just for reading its input stream? – pskink Oct 18 '18 at 18:04
  • 1
    @pskink The file should be uploaded to a server. The upload method takes a `File` as parameter. Your hint did not help me, I am still researching :) – Manuel Oct 18 '18 at 18:05
  • using okhhtp? if so, use a custom `RequestBody` – pskink Oct 18 '18 at 18:08
  • @pskink No, using Parse Android SDK. – Manuel Oct 18 '18 at 18:08
  • so you cannot do that as it is not possible to get a `File` object from a given `Uri` – pskink Oct 18 '18 at 18:15
  • I found some hacky ways with flaws depending on the content provider. The best ways seems to be to pass `File` objects around instead of `URI`s - which I will do instead. – Manuel Oct 18 '18 at 18:19
  • yes, it would be the best option in that case – pskink Oct 18 '18 at 18:24

0 Answers0