Exception 'open failed: EACCES (Permission denied)' on Android

前端 未结 30 1630
[愿得一人]
[愿得一人] 2020-11-22 00:59

I am getting

open failed: EACCES (Permission denied)

on the line OutputStream myOutput = new FileOutputStream

30条回答
  •  旧时难觅i
    2020-11-22 01:49

    My issue was with "TargetApi(23)" which is needed if your minSdkVersion is bellow 23.

    So, I have request permission with the following snippet

    protected boolean shouldAskPermissions() {
        return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
    }
    
    @TargetApi(23)
    protected void askPermissions() {
        String[] permissions = {
                "android.permission.READ_EXTERNAL_STORAGE",
                "android.permission.WRITE_EXTERNAL_STORAGE"
        };
        int requestCode = 200;
        requestPermissions(permissions, requestCode);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    // ...
        if (shouldAskPermissions()) {
            askPermissions();
        }
    }
    

提交回复
热议问题