解决安卓Q(10)open failed: EACCES (Permission denied)问题

1.在Manifest.xml添加

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

2.动态权限获取

private void requestPermission(){

    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ //表示未授权时
        //进行授权
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
    }else{
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case 1:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ //同意权限申请
            }else { //拒绝权限申请
                Toast.makeText(this,"权限被拒绝了",Toast.LENGTH_SHORT).show();
            }
            break;
        default:
            break;
    }
}

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestPermission();
        .
        .
        .
}

3.安卓10谷歌新添加了一项限制

相关说明:
Scoped Storage
Q: 应用如何暂时豁免 Q 的外部存储沙箱限制。

A: 不会。所以我们强烈建议应用在 Q 版本时作出适配,但如您需要多点时间,可考虑在 Manifest 中增加 application android:requestLegacyExternalStorage = “true” 如果要检测是否已豁免,使用 Environment.isExternalStorageLegacy() 函数。

<application
			.
			.
			.
			
        android:requestLegacyExternalStorage="true"
        	.
        	.
        	.
        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>

你可能感兴趣的:(解决安卓Q(10)open failed: EACCES (Permission denied)问题)