打开失败:EACCES(权限被拒绝)第一个 运行
open failed: EACCES (Permission denied) on first run
在 运行 时间请求所有权限后,我正在尝试 write/read 将文件放入外部存储。
清单:
<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" />
运行时间:
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE.
Java代码:
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;
}
}
加载文件:
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;
}
我第一次 运行 我打开的应用程序失败:EACCES(权限被拒绝)仅在 Android 6.0.1,但是,在其他版本的操作系统中,功能运行良好。如果我在 6.0.1 上终止并重新打开应用程序,该功能可以正常工作。
Android 6.0 有一个错误,在所有应用程序进程被杀死之前,权限不会被应用。在其他版本的操作系统中,当权限设置发生变化时,应用程序会自动终止并从上次 Activity.
重新启动
我避免了在 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();
}
}
}
在 运行 时间请求所有权限后,我正在尝试 write/read 将文件放入外部存储。
清单:
<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" />
运行时间:
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE.
Java代码:
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;
}
}
加载文件:
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;
}
我第一次 运行 我打开的应用程序失败:EACCES(权限被拒绝)仅在 Android 6.0.1,但是,在其他版本的操作系统中,功能运行良好。如果我在 6.0.1 上终止并重新打开应用程序,该功能可以正常工作。
Android 6.0 有一个错误,在所有应用程序进程被杀死之前,权限不会被应用。在其他版本的操作系统中,当权限设置发生变化时,应用程序会自动终止并从上次 Activity.
重新启动我避免了在 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();
}
}
}