Android 10: Delete denied by permission:android.permission.ACCESS_MEDIA_PROVIDER - android

Target SDK in my project is 31 and when I'm trying to delete a file from DCIM directory, I'm getting following error such as delete denied by permission:android.permission.ACCESS_MEDIA_PROVIDER.
Please consider file path like this: "/storage/emulated/0/DCIM/Screenshots/pic1.jpg"
I have tried so much to find the result every where including stack overflow, but didn't succeeded yet. Even I changed target SDK to lower 30 but not worked.
Following are the solutions that I had already worked on but nothing works:
1.flags in manifest file
android:requestLegacyExternalStorage="true"
android:preserveLegacyExternalStorage="true"
2.permissions in manifest file
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="40"
tools:replace="android:maxSdkVersion" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
Please find the attached code
val fileToDelete = File("/storage/emulated/0/DCIM/Screenshots/pic1.jpg")
fileToDelete.delete()
NOTE: filepath is hardcoded only to explain better
Please find the attached log
2022-03-21 11:22:35.331 8639-20226/com.filepickerdemo D/ContentResolver: delete denied by permission:android.permission.ACCESS_MEDIA_PROVIDER#content://media/external/images/media#_data = ?#/storage/emulated/0/DCIM/Screenshots/pic1.jpg
What else should I put in my code to solve this issue. Thanks in advance.

To delete media files in android 10 and above, You can try contentresolver API.
You need to pass the media's content uri to the 'contentResolver.delete()' method & you will be able to do it easily.
Here is my post on how to do that using Java - Scoped Storage in Android — Writing & Deleting Media Files

Related

Android assets no such file or directory

In my android project I have created an assets folder in which I have added some json files and I want to read them with a stringBuffer. The assets folder is listed inside src/main. So far I have added the following code:
String[] files = assetManager.list("");
ArrayList<String> it = new ArrayList<>(Arrays.asList(files));
And so far I am getting the names of the files successfully inside my ArrayList. Then I am trying this:
InputStream input = assetManager.open("filename.json");
But I get the following error:
Failed to open file
'/data/data/package/code_cache/.overlay/base.apk/assets/filename.json':
No such file or directory
I also have included the follwing permission in the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Any idea what might be the problem here?
You can clear storage in device and then rebuild app, that help my same issue.
I got the same issue on my Android project many times. Try uninstalling the app on your emulator and rebuild your app.

Android untar a file permission denied

I have a tar file that I want to download from an API and un-tar it, on most devices the code below works fine but on few devices it gives an error
code:
val untar = arrayOf("tar", "-xvf", file.absolutePath, "-C", tempDirectory.absolutePath)
val process = Runtime.getRuntime().exec(untar)
Error on few devices only:
tar: exec gunzip: Permission denied
Make sure you have the right android permissions:
<manifest>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
I am not sure, maybe tar which is a link to toybox is fine, but unzip which is a link to ziptool is not. Maybe you should try using apache commons compress, in order to bring your own dependecies and not rely on operation system issues, altough the solution is going to be more complex.

How can we access all files from download directory in android 11 and above

In below android 11 we can get the all files from public directory(Downloads) using File(getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)).listFiles() but in android 11 when i try to access files am getting files which are related to my application am unable to get all the files and how can we get all the files in android 11
am using this both permissions for reading and writing file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
this is snippet for getting list
val files: Queue<File> = LinkedList()
val ROOT_DIR = getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).absolutePath
files.addAll(File(ROOT_DIR).listFiles())
Here is the full description on security updates for external storage access in Android 11 and above.
https://developer.android.com/about/versions/11/privacy/storage
You have two options.
Or use 'all files access' using permission MANAGE_EXTERNAL_STORAGE.
Or let the user select the files using ACTION_,OPEN_DOCUMENT. Allow multiple.

Myo android first connection

I am very new to android. I am trying to follow this Myo android tutorial
But I am facing a lot of problems since I don't know a lot about android.
My first problem is with placing the sdk , I downloaded it, and it is written that I have to add the SDK as a dependency in my build.gradle file.
When I explored my project, I found 2 build.gradle files as in the image:
So should I add it in which one ?
And how should I add it as an absolute path so I can use the SDK on all devices without changing the path ?
And then they asked me to add these permissions, but where should I add them?
// Required for communicating with the Myo device
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
// Required for sending usage data to Thalmic Labs
<uses-permission android:name="android.permission.INTERNET" />
Q1:So should I add it in which one ?
A:You should add it in the "Module:app" one.
Q2:how should I add it as an absolute path so I can use the SDK on all devices without changing the path ?
A:The path of the SDK doesn't relate to the result,the SDK will be wrapped up in the apk,so don't worry about it.
Q3:Where should I add them (permissions)
A:Add them in AndroidManifest.xml.

Create folder on /mnt/externel1

I want to create folder on /mnt/externel1 (which is my external micro-sd card path) but when I create folder problematically [ file.mkdirs() ] it is returning false.
And when I am trying to download a file on that path by creating an outputStream it throwing an exception "Permission denied"
Note: android application not allowing to write on external micro-sd card.
your advise will helpful for me.
Please add below permission in manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
you have to mentioned permission in manifest file when you want to write into sdcard.
If you're targeting Honeycomb, you can't write to the external SD card.
Edit
Just noticed the permission stuff - you should make sure that you've got the appropriate permissions. See Chirag Raval's answer.

Categories

Resources