问题
I am trying to write a file to the external storage location on Android. Here is my code :
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)) {
saveState= true;
File pPath= Environment.getExternalStorageDirectory();
if(!pPath.exists()) {
boolean bReturn= pPath.mkdirs();
Log.d(TAG, "mkdirs returned: " + bReturn);
}
try {
File pFile= new File(pPath, "output.pcm");
pFile.createNewFile();
outputLocation= pFile.getAbsolutePath().toString();
} catch (IOException e) {
Log.d(TAG, "Could not create file: " + e.toString());
saveState= false;
}
} // end if we can read/write to the external storage
The call to createNewFile() returns "Could not create file: java.io.IOException: Permission denied"
I have the android.permission.WRITE_EXTERNAL_STORAGE in my manifest file. There seems to be a ton of questions about this problem, and this solved most of them, but I am still having this issue. Here is part of the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="company.example.sample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<permission android:name="android.permission.RECORD_AUDIO"></permission>
<permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"></permission>
<permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></permission>
Any ideas?
回答1:
Replace your <permission>
elements with <uses-permission>
elements:
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<permission>
defines a new permission; <uses-permission>
indicates you want to hold a permission.
来源:https://stackoverflow.com/questions/9931662/android-permission-denied-when-creating-a-new-file-in-external-storage