'How to implement New Media Storage API to store file in Android 11 and 12?

i am trying to store the video on Device Storage and showing video in app but android version like 10,11 and 12 it is showing error that not able to create folder in phone.

My Error

 java.io.IOException: Operation not permitted
at java.io.UnixFileSystem.createFileExclusively0(Native Method)
at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:317)
at java.io.File.createNewFile(File.java:1006)
at com.pixeldev.videoapp.helper.DownloadManager.onHandleIntent(DownloadManager.java:90)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:78)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.os.HandlerThread.run(HandlerThread.java:67)`

My Code

My code Screenshot



Solution 1:[1]

Use Public Directory inside Folder Like..

File myDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "MyPublicFolder");
myDirectory.mkdir();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vishal Bhut