1

I am writing a native module for react-native app. I’m new to react-native and android development in general. Inside of a module class I created a method that opens camera activity by using intent MediaStore.ACTION_VIDEO_CAPTURE and calling startActivityForResult like this:

    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    Bundle bundle = new Bundle();
    reactContext.startActivityForResult(intent, 1);

This code works. However, I need to access all videos recorded with this app, so I decided that storing them in app-specific folder is more suitable then providing users access to all videos on a file system (default saving location shares space with all other video files on a system). As suggested by documentation I decided to use intent extra MediaStore.EXTRA_OUTPUT.

So I added the following code:

    File folder = reactContext.getFilesDir();
    File file = new File(folder+"/test.mp4");
    Uri uri = Uri.fromFile(file);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);

Running this code fails with the following error:

file:///data/user/0/com.myapp/files/test.mp4 exposed beyond app through ClipData.Item.getUri()

After googling this error, I found this question. Accepted answer explains that this error is thrown since sdk 24 (I test on a device of sdk 30) when you get the uri of a file outside your app storage.

I conclude that I don’t quite understand what is supposed to be an app’s storage. According the documentation I assume that what is returned by context.getFilesDir() satisfies definition of an app storage. But it seems like I am wrong.

There are also two suggested solutions (changing API policy or using FileProvider) to use in order to ”just make it work”, but the question I am asking is: "What am I missing with understanding the idea of app storage?" Or maybe, there’s another API restriction arrived with later versions of sdk (as I mentioned I test on a device with sdk 30)? Or it is due to some react-native reason?

I would appreciate references to specific sections of android or react-native documentation or any other explanation to the described situation.

Thanks in advance.

MURIMO
  • 23
  • 1
  • 6
  • Do not use Uri.fromFile(). Instead use FileProvider class and getUriForFile() to get a usable uri. – blackapps Jun 06 '23 at 16:00
  • `I am asking is: "What am I missing with understanding the idea of app storage?" ` Then why all this talking about taking a picture? Confusing. – blackapps Jun 06 '23 at 16:02
  • There is no app storage. The device has storage locations for files. Some of these are app specific locations. – blackapps Jun 06 '23 at 16:03
  • @blackapps, What I was wondering is if saving a recorded video to `context.getFilesDir()` (returned file) is doable without using FileProvider. Am I understanding correctly that it is impossible without FileProvider? I'm sorry if that is supposed to be obvious, but I just keep reading in different guides that FileProvider is used for sharing files with other apps which is not the functionality I need. – MURIMO Jun 06 '23 at 18:13
  • 1
    If your app wants to create a file in getFilesDir() then it can just do it. But if your app wants to use an external app to create a video file there then it should present an uri to that app. The uri can come from FileProvider or your own content provider. The content provider in this case acts as a content receiver. – blackapps Jun 06 '23 at 19:55
  • @blackapps, Thank you! – MURIMO Jun 08 '23 at 16:52

0 Answers0