I want to read a bmp file in ndk code, but it always failed.and the error code showed the permission denied. how to solve this problem?
The IDE is Android Studio 3.1.2,but I think it is nothing to do with IDE. The device is hikey960 with Android 9 API28.
I have referenced some similay issues and added the user-permission in Manifest.xml and specify the absolute path, but it still doesn't work! But if I build it as a executalbe file and push to /data/ and give a full permission, it works normally!
here is the ndk code:
FILE *fpbmp = fopen (imagefile, "r+");
if (fpbmp == NULL) {
LOGD ("open file failed!");
LOGD("code %s \n", strerror(errno));
}
and the AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wangzh.normaltest">
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
....
The logcat shows that the error code means permission denied:
/sdcard/DCIM/1_1.bmp
open file failed!
code Permission denied
Could someone give some advices? Anything will be appreciated!
I think I find the reason!
the permission of the bmp file in /sdcard/DCIM/ is -rw-rw----. It can't be changed even though run the chmod command and set permission in AndroidManifest.xml and run-time.
So I mv file to /data/ and chmod 777 thefile. then it work well and doesn't need permission in AndroidManifest.xml or run-time permission.
on the console, input the below command:
su
setenforce 0
You can use the below command to check if the command is inputed before.
getenforce
if it return "Permissive", it is ok.
I use android 11, cpp code is at hidl interface. With this method, it is solved. fopen/fwrite/fread work well.
Related
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
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.
I'm attempting to write a simple log file from inside av/services/audioflinger/AudioStreamOut.cpp, for testing a custom ROM.
I need it to work on Nexus 5X (bullhead).
if (logFile1 == NULL)
logFile1 = fopen("/sdcard/log_before.pcm", "ab");
if (logFile1 == NULL)
ALOGE("can't open 1, errno %d", errno);
I'm getting permission denied (ERRNO is 13).
I tried modifying frameworks/base/data/etc/platform.xml by adding the following:
<assign-permission name="android.permission.WRITE_EXTERNAL_STORAGE" uid="media" />
<assign-permission name="android.permission.WRITE_EXTERNAL_STORAGE" uid="audioserver" />
<assign-permission name="android.permission.WRITE_EXTERNAL_STORAGE" uid="audio" />
Still no change.
How can I get this to work? Or is there another place in the filesystem where a system service like audioflinger is supposed to use for writing files?
UPDATE:
I just found a solution that works for emulator - in Android N, write to /data/misc/audioserver.
When I use ADB shell, it shows me that this particular folder has the "audioserver" group, which is why it works.
I found about this folder from this link: https://source.android.com/devices/audio/debugging.html
But this folder seems not accessible on Nexus 5X - can't get to it with shell, even with su root.
I found a way around the permission issue.
First, write the log file to /data/misc/audioserver
Then:
adb shell su root cp /data/misc/audioserver/log_before.pcm sdcard/.
Etc.
And then I can pull the file:
adb pull /sdcard/log_before.pcm .
Goal:
Taking a screenshot during an automated test on a device. Pull the screenshot file using adb once the test is done.
Context:
I'm currently trying to write automated tests to take snapshots of the device screen. Using UiDevice to navigate, I would like to take a screenshot in the middle of a test. UiDevice has a method takeScreenshot that I call when I would like to take a snapshot.
After some investigation, I realised that the class responsible to write the image into file UiAutomatorBridge catches an Exception :
java.io.FileNotFoundException:
/data/local/tmp/screenshots/screen2.png: open failed: EACCES
(Permission denied)
Using adb, I created the file and set all permissions to all users.
adb shell touch /data/local/tmp/screenshots/screen1.png
adb shell chmod 777 /data/local/tmp/screenshots
Once done, I can take a screenshot with :
#Test
public void takeSnapShot() {
String filename = "/data/local/tmp/screenshots/screen1.png";
File file = new File(filename);
assertEquals(true, mDevice.takeScreenshot(file));
}
Problem :
I would like to be able to create a file directly while the test is executing, without the need of using adb.
I tried to create a file directly from Java using createNewFile.
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
But I get an IOException
java.io.IOException: open failed: EACCES (Permission denied)
Has anyone an idea of what is going on ? I'm quite new to Linux, so don't hesitate to suggest something even if it seems obvious to you ;)
Should I post this on superuser instead ?
EDIT
The directory /data has these permissions
drwxrwx--x system system 2016-01-14 14:03 data
I can't list the content of /data, which makes me believe the user "shell" doesn't belong to the group "system". I can't list the content of /data/local neither.
However, the /data/local/tmp is owned by "shell".
drwxrwx--x shell shell 2016-01-14 12:20 . (tmp)
/data/local/tmp gives +x permission to all users.
Finally, the directory "screenshots" belongs to shell with permissions 777.
drwxrwxrwx shell shell 2016-01-14 11:46 screenshots
To my understanding, any user should be able to access /data/local/tmp/screenshots
Instead of saving under /data/local/tmp/, use Environment.getExternalStorageDirectory().
You need a read/write permission to do that. You have to add these two lines into the Manifest of the application ! (I tried to add those lines into the /androidTest/Manifest.xml file, but it has no effects).
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
While it solves the problem of finding a common directory to save/read screenshots, it brings a new one as well.
Having to add these 2 permissions isn't a big deal if you already had them. But if you don't want to ask the user for those permissions, you have to add/remove these lines every time you test/sign your application, which is far from ideal.
EDIT :
In this post, someone proposed to make use of the build differentials (debug, release) to have two different Manifests.
In the end, I have 3 Manifests :
Release Manifest (without external read/write permissions)
Debug Manifest (with external read/write permissions)
AndroidTest Manifest for
tools:overrideLibrary="android.support.test.uiautomator.v18"
There is a folder called "D_Permision" SD Card and there is a text file called "permission.txt" in it.
I want to read this text file through command prompt and display its content in the command prompt(Not in a separate file)
When I use
adb pull permission.txt /sdcard/D_Permission
it gives following error
remote object 'permission.txt' does not exist.
but I used
adb push permission.txt /sdcard/D_Permission
(successfully added permission.txt to D_Permission folder)
and the tried to pull.but I get same error.
sdcard/D_permission has both read / write permission
So how to read this text file through command prompt and display its content in the cmd?
You don't need to pull the file.
adb provides shell acces, so you can do a cat:
adb shell cat /sdcard/D_Permission/foo.txt
If the file does not exist though, you'll still get "does not exists" error of course, which is normal.
I directly use the path "/mnt/external_sd/.. Path of File.".
But it was only work with my Device
Maybe you forget these permissions.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />