简体   繁体   中英

open failed: EACCES (Permission denied) on first run

I'm trying to write/read a file into the external storage after requesting all permissions on runtime.

Manifest:

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

Runtime:

Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE.

JavaCode:

    public class FilesUtil {

    public static String saveImageToFile(Bitmap image ,String employeeId){
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/AppName");
        myDir.mkdirs();
        String filename = String.format("EMPLOYEE_%s.png", employeeId);
        File file = new File (myDir, filename);
        if (file.exists()){
            file.delete(); // here i'm checking if file exists and if yes then i'm deleting it but its not working
        }

        FileOutputStream out = null;
        try {
            out = new FileOutputStream(file,false);
            image.compress(Bitmap.CompressFormat.PNG, 100, out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return filename;
    }

    public static String getImagePath(String employeeId){
        String result  = Environment.getExternalStorageDirectory().toString() + "/AppName/";
        String filename = String.format("EMPLOYEE_%s.png", employeeId);
        return result + filename;
    }
}

Load File:

private Bitmap getCurrentPhoto() {
    File image = new File(FilesUtil.getImagePath(getCurrentEmployeeId()));
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(), bmOptions);
    return bitmap;
}

The first time that I run the app I get an open failed: EACCES (Permission denied) just on Android 6.0.1, However, in other versions of the Operative system, the feature is working fine. If I kill and Reopen the app on 6.0.1, the feature is working fine.

There is a bug on Android 6.0, the permissions are not being applied until all the application processes are killed. In other versions of the operating system when there is a change in the permissions settings, the App is Killed automatically and restarted from last Activity.

I avoid the bug using this on onRequestPermissionsResult and Restart the App.

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (grantResults.length == permissions.length){
        if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage(R.string.restart_message)
                    .setPositiveButton(R.string.restart_button, (dialog, id) -> {
                        restartApp();
                    });
            builder.create().show();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM