Android: alternative to deprecated Context.MODE_WORLD_READABLE?

From here I know a way to write a file and be accessible to another application and another intention, but now that Context.MODE_WORLD_READABLE is deprecated, how can I safely do this?

FileOutputStream out = myActivity.openFileOutput(fileTo, Context.MODE_WORLD_READABLE); 

Ok more info:

I use this:

 intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "video/*"); 

And uri will be the one that I will write in the SD card. And the video will appear from the application, so the problem is that if it is not allowed now, how can I write a file and view it.

+13
source share
3 answers

And uri will be the one that I will write in the SD card.

This is already MODE_WORLD_WRITABLE by default. Also note that the code you specified ( openFileOutput() ) is not written to external storage (that you are not calling sdcard correctly). openFileOutput() is for internal storage.

And the video will appear from the application, so the problem is that if it is not allowed now, how can I write a file and view it.

If you really write the file in external storage, just use the Uri pointing to that file.

If you are writing a file to internal memory, create a ContentProvider to serve this file and use the Uri pointing to this ContentProvider . Here is an example application with ContentProvider that extracts a PDF file from assets/ on first start, then serves this file via openFile() , so it can be viewed by a PDF viewer.

+11
source

Save the video to your internal memory using:

 openFileOutput("test.mp4", "MODE_PRIVATE"); 

Then do the following:

 String path = context.getFilesDir().getAbsolutePath() + "/test.mp4"; // path to the root of internal memory. File f = new File(path); f.setReadable(true, false); Intent playIntent .... playIntent.setType("video/*"); playIntent.setData(Uri.fromFile(f)); 

Good luck.

+7
source

It seems that the documents in it are understandable.

This constant was deprecated at API level 17.

Creating public files is very dangerous and can cause security holes in applications. This is very discouraging; instead, applications should use a more formal interaction mechanism, such as ContentProvider, BroadcastReceiver, and Service. No guarantee that this access mode will remain in the file, for example, when it goes through backup and restore. File creation mode: allow all other applications have read access to the created file.

+3
source

Source: https://habr.com/ru/post/1210846/


All Articles