![](/img/trans.png)
[英]W/System.err: java.io.IOException: Cannot run program "python": error=13, Permission denied
[英]W/System.err: java.io.IOException: Permission denied when writing file
我正在嘗試創建一個文件來存儲 AudioRecord 收集的數據。 但是當我這樣做時,android studio 告訴我:
04-09 21:23:47.665 28293-28293/com.example.gongk.testfmcwv3 W/art: Before
Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
04-09 21:23:47.845 28293-28293/com.example.gongk.testfmcwv3 W/System.err: java.io.IOException: Permission denied
04-09 21:23:47.846 28293-28293/com.example.gongk.testfmcwv3 W/System.err: at java.io.UnixFileSystem.createFileExclusively0(Native Method)
04-09 21:23:47.846 28293-28293/com.example.gongk.testfmcwv3 W/System.err: at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
04-09 21:23:47.846 28293-28293/com.example.gongk.testfmcwv3 W/System.err: at java.io.File.createNewFile(File.java:948)
04-09 21:23:47.846 28293-28293/com.example.gongk.testfmcwv3 W/System.err: at com.example.gongk.testfmcwv3.MainActivity.init(MainActivity.java:82)
04-09 21:23:47.846 28293-28293/com.example.gongk.testfmcwv3 W/System.err: at com.example.gongk.testfmcwv3.MainActivity.onCreate(MainActivity.java:63)
04-09 21:23:47.846 28293-28293/com.example.gongk.testfmcwv3 W/System.err: at android.app.Activity.performCreate(Activity.java:6912)
04-09 21:23:47.846 28293-28293/com.example.gongk.testfmcwv3 W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
04-09 21:23:47.846 28293-28293/com.example.gongk.testfmcwv3 W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900)
04-09 21:23:47.846 28293-28293/com.example.gongk.testfmcwv3 W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
04-09 21:23:47.846 28293-28293/com.example.gongk.testfmcwv3 W/System.err: at android.app.ActivityThread.-wrap14(ActivityThread.java)
04-09 21:23:47.846 28293-28293/com.example.gongk.testfmcwv3 W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
04-09 21:23:47.846 28293-28293/com.example.gongk.testfmcwv3 W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
04-09 21:23:47.846 28293-28293/com.example.gongk.testfmcwv3 W/System.err: at android.os.Looper.loop(Looper.java:154)
04-09 21:23:47.846 28293-28293/com.example.gongk.testfmcwv3 W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6688)
04-09 21:23:47.846 28293-28293/com.example.gongk.testfmcwv3 W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-09 21:23:47.846 28293-28293/com.example.gongk.testfmcwv3 W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
04-09 21:23:47.846 28293-28293/com.example.gongk.testfmcwv3 W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
04-09 21:23:47.866 28293-28300/com.example.gongk.testfmcwv3 W/art: Suspending all threads took: 5.158ms
我的 AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gongk.testfmcwv3">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
代碼:
file = new File(Environment.getExternalStorageDirectory(),"TestFMCWR");
if (file.exists()) file.delete();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
我無法弄清楚為什么權限不起作用。
如果您的目標 api 級別 >=29 或者您使用的是 android 10,那么請在應用程序標簽的應用程序清單文件中添加以下行
android:requestLegacyExternalStorage="true"
嘗試這個,
private Context mContext=YourActivity.this;
private static final int REQUEST = 112;
if (Build.VERSION.SDK_INT >= 23) {
String[] PERMISSIONS = {android.Manifest.permission.READ_EXTERNAL_STORAGE,android.Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (!hasPermissions(mContext, PERMISSIONS)) {
ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST );
} else {
//do here
}
} else {
//do here
}
獲取權限結果
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//do here
} else {
Toast.makeText(mContext, "The app was not allowed to read your store.", Toast.LENGTH_LONG).show();
}
}
}
}
檢查棉花糖的權限
private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
顯現
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
我遇到了同樣的問題。 換句話說,我的清單似乎擁有所有正確的權限,而且我在 API 級別 22 上運行沒有問題。當我安裝在 API 級別 25 的設備上時,我的相機功能停止工作。 我可以在日志中看到我遇到了與 OP 相同的異常。
這是通過進入設備上的應用程序設置並允許“存儲”權限來解決的。 無需更改代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.