【问题标题】:W/System.err: java.io.IOException: Permission denied when writing fileW/System.err:java.io.IOException:写入文件时权限被拒绝
【发布时间】:2017-04-10 02:35:54
【问题描述】:

我正在尝试创建一个文件来存储 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>

java代码:

file = new File(Environment.getExternalStorageDirectory(),"TestFMCWR");
        if (file.exists()) file.delete();
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

我不知道为什么权限不起作用。

【问题讨论】:

  • 如果您的应用程序安装在设备 API 23 (Marshmallow) 及更高版本中,您需要请求用户允许权限。
  • 顺便说一下,显示的错误与权限被拒绝无关
  • 在我的情况下就像.i check write_external_storage permission .enable read_external_storage 。这需要很长时间才能找出问题所在。我知道我添加了运行时权限,并且我验证了通过打开应用设置激活的权限。

标签: java android


【解决方案1】:

如果您的目标 api 级别 >=29 或者您使用的是 android 10,请在应用程序标记的应用清单文件中添加以下行

android:requestLegacyExternalStorage="true"

【讨论】:

    【解决方案2】:

    试试这个,

        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" />
    

    【讨论】:

    • 感谢您的回答。我已经修复了那个错误。我在 Android 开发者网站上阅读了关于在运行时请求权限的说明。而且我发现我也可以将 Gradle 中的目标版本更改为 22(小于 23)。无论如何感谢您的帮助!
    • 在明确接受这两个权限后仍然出现此错误
    【解决方案3】:

    我遇到了同样的问题。换句话说,我的清单似乎拥有所有正确的权限,并且我在 API 级别 22 上运行没有问题。当我安装在 API 级别 25 的设备上时,我的相机功能停止工作。我可以在日志中看到我遇到了与 OP 相同的异常。

    通过进入设备上的应用设置并允许“存储”权限解决了这个问题。无需更改代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-01
      • 2015-11-24
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多