W /系统。错:io。IOException:写入文件时被拒绝的权限。

时间:2022-05-21 23:01:00

I'm trying to create a file to store the data collected by AudioRecord. But when I do that, android studio show me that:

我正在尝试创建一个文件来存储AudioRecord收集的数据。但当我这么做的时候,安卓工作室告诉我:

 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

My AndroidManifest.xml:

我的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 code:

java代码:

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

I can't figure out why the permission is not work.

我搞不懂为什么这个许可不起作用。

2 个解决方案

#1


0  

Try this,

试试这个,

    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
    }

get Permissions Result

得到许可的结果

    @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();
                }
            }
        }
    }

check permissions for marshmallow

检查权限的棉花糖

    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;
    }

Manifest

清单

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

#2


0  

I was experiencing this same issue. In other words, my manifest appeared to have all the correct permissions, and I was running without issues on API level 22. When I installed on a device with API level 25, my camera feature stopped working. I could see in the logs that I was getting your same exception as OP.

我也遇到过同样的问题。换句话说,我的清单显示了所有正确的权限,而我在API级别22上没有问题。当我安装了一个API级别25的设备时,我的相机功能停止了工作。我可以在日志中看到,我得到了与OP相同的异常。

This was solved by going into the app's settings on the device and allowing "Storage" permission. No code changes needed.

通过在设备上的设置和允许“存储”权限来解决这个问题。不需要更改代码。

#1


0  

Try this,

试试这个,

    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
    }

get Permissions Result

得到许可的结果

    @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();
                }
            }
        }
    }

check permissions for marshmallow

检查权限的棉花糖

    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;
    }

Manifest

清单

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

#2


0  

I was experiencing this same issue. In other words, my manifest appeared to have all the correct permissions, and I was running without issues on API level 22. When I installed on a device with API level 25, my camera feature stopped working. I could see in the logs that I was getting your same exception as OP.

我也遇到过同样的问题。换句话说,我的清单显示了所有正确的权限,而我在API级别22上没有问题。当我安装了一个API级别25的设备时,我的相机功能停止了工作。我可以在日志中看到,我得到了与OP相同的异常。

This was solved by going into the app's settings on the device and allowing "Storage" permission. No code changes needed.

通过在设备上的设置和允许“存储”权限来解决这个问题。不需要更改代码。