昨天本来想写一个demo,然后放到手机上用神之手hook,然后用我的hookui测试批量修改集合内模型数据,没想到这么简单的问题都搞不定了。。
权限申请了,清单文件也定义了,也手动进入设置给了存储权限,然后今天动态调用了权限申请了,
还是提示EPERM operation not permitted
网络上说的的在application节点
android:requestLegacyExternalStorage="true"
前几天我用这个方法解决了,可能是因为编译tagetSdk版本不够高吧,才勉强解决了问题,但是今天这样设置也没效果了,。
进入清单文件选择
WRITE_EXTERNAL_STORAGE no longer provides write access when targeting Android 11+, even when using requestLegacyExternalStorage
于是根据这个关键词搜索才找到解决方案,
解决方法如下:
java层对于android 11的代码
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && false == Environment.isExternalStorageManager()) {
Uri uri = Uri.parse("package:" + BuildConfig.APPLICATION_ID);
startActivity(new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, uri));
}else{
callPermissionLogic(this, 0);
}
我写的完整代码,不用任何框架
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && false == Environment.isExternalStorageManager()) {
Uri uri = Uri.parse("package:" + BuildConfig.APPLICATION_ID);
startActivity(new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, uri));
}else{
callPermissionLogic(this, 0);
}
}
private static final int REQUEST_CODE_SELECT_PLUGIN = 1;
private boolean mChanage;
private int _CurrentPermissionIndex;
List waitRequestPermissionList = new ArrayList<>();
{
waitRequestPermissionList.add(Manifest.permission.READ_EXTERNAL_STORAGE);
waitRequestPermissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
private int callPermissionLogic(Activity activity, final int from) {
int breakFlag = -1;
for (int i = from; i < waitRequestPermissionList.size(); i++) {
final String currentPermission = waitRequestPermissionList.get(i);
_CurrentPermissionIndex = i;
final int finalI = i;
if (PermissionUtil.checkSelfPermission(this, currentPermission) != PackageManager.PERMISSION_GRANTED) {
breakFlag = i;
if (PermissionUtil.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Log.w("PermissionRequest", "shouldShowRequestPermissionRationale call");
Toast.makeText(activity, "权限被拒绝" + currentPermission + ",你可以再次申请", Toast.LENGTH_SHORT).show();
callPermissionLogic(activity, from + 1);
} else {
PermissionUtil.requestPermissions(this, new String[]{currentPermission}, i);
if (BuildConfig.DEBUG) {
Log.w("PermissionRequest", "第一次或者最后一次权限申请 " + currentPermission);
}
}
} else {
if (BuildConfig.DEBUG) {
Log.w("PermissionRequest", "权限" + currentPermission + "已拥有...");
}
}
}
return breakFlag;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
PermissionUtil.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
//onRequestPermissionsResult requestCode:1,permission:[android.permission.FOREGROUND_SERVICE],grantResult:[-1]
if (BuildConfig.DEBUG) {
Log.w("PermissionRequest", "onRequestPermissionsResult requestCode:" + requestCode + ",permission:" + Arrays.toString(permissions) + ",grantResult:" + Arrays.toString(grantResults));
}
_CurrentPermissionIndex++;
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
} else {
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
在上面代码可以找到如下行
Toast.makeText(activity, "权限被拒绝" + currentPermission + ",你可以再次申请", Toast.LENGTH_SHORT).show();
可以替换成下面的再次调用
Log.w("PermissionRequest", "shouldShowRequestPermissionRationale call");
DialogUtils.showConfirmDialog(this, "年轻人,权限不用轻易拒绝啊,再给你一次机会,我需要申请" + currentPermission + "权限,这个权限有利于机器人程序得问题哈!", new INotify() {
@Override
public void onNotify(Void param) {
PermissionUtil.requestPermissions(activity, new String[]{currentPermission}, finalI);
}
}, new INotify() {
@Override
public void onNotify(Void param) {
callPermissionLogic(activity,from + 1);
}
});
关于android11的问题看看大神们怎么讨论的。
https://stackoverflow.com/questions/64221188/write-external-storage-when-targeting-android-10