[英]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: 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. 我第一次运行该应用程序时,打开失败:仅在Android 6.0.1上运行EACCES(权限被拒绝),但是,在其他版本的Operative系统中,该功能运行正常。 If I kill and Reopen the app on 6.0.1, the feature is working fine. 如果我在6.0.1上杀死并重新打开了该应用程序,则该功能运行正常。
There is a bug on Android 6.0, the permissions are not being applied until all the application processes are killed. Android 6.0上存在一个错误,直到所有应用程序进程都被杀死,该权限才被应用。 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. 我避免在onRequestPermissionsResult上使用此错误并重新启动应用程序。
@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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.