我想在外部存儲器中創建一個文件,代碼如下:
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;
}
}
然後調用 createNewFile()時返回
"Could not create file: java.io.IOException: Permission denied"
在 manifest 文件中有 android.permission.WRITE_EXTERNAL_STORAGE。
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>
為什麼會說在外部存儲器沒有權限創建一個新的文件呢?
改成:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission >