solution: test target project must also add WRITE_EXTERNAL_STORAGE in AndroidManifest.xml
this question came from an android test project,which can't write test result into SDcard. Add WRITE_EXTERNAL_STORAGE into test target project can solve this problem.
Always throw permission denied when I use code below to write xml file into emulator's sdcard. I have added WRITE_EXTERNAL_STORAGE" in AndroidManifest.xml.
@Override
public void onStart(){
try {
File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
myWriter = new FileWriter(new File(root, TEST_RESULT+".xml"));
startResultOutput(myWriter);
} catch (IOException e) {
Log.d("TestInfo", "after new FileWriter: "+e.getMessage());
}
super.onStart();
}
catched IOExcetion, the message was: after new FileWriter: /mnt/sdcard/InterFace_test_result.xml (Permission denied)
Below is the manifest code
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="android.test.runner" />
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS"></uses-permission>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
SD card was mounted, because I can use another application to write file into external sd card.
Can anybody give me some suggestion?
PS: the emulator system is 2.3.3
To read and write data to external storage, the app required WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE system permission. These permissions are added to the AndroidManifest. xml file. Add these permissions just after the package name.
WRITE_EXTERNAL_STORAGE does not give any privileges in Android 11 (API 30), it does nothing at all, therefore in API 11 you need to set the maxSdkVersion to 29. If in Android 10 (API 29) you are also not using requestLegacyExternalStorage then set maxSdkVersion to 28 instead of 29.
Add declaration to app manifest To declare a permission that your app might request, include the appropriate <uses-permission> element in your app's manifest file. For example, an app that needs to access the camera has this line in AndroidManifest. xml : <manifest ...>
Problem:
What I can understand is,
You are adding permission
two times
Application
tag as an attribute<use-permission>
Solution:
Try removing any one of them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With