当前位置:网站首页>解决万恶的open failed: ENOENT (No such file or directory)/(Operation not permitted)
解决万恶的open failed: ENOENT (No such file or directory)/(Operation not permitted)
2022-07-02 06:25:00 【Linxi林夕】
一、Android 11上传下载资源失败:open failed: ENOENT (No such file or directory)
前提:发现Android 11 上传图片及下载文件都失败,报错日志:open failed: ENOENT (No such file or directory)
原来是Android 11访问文件资源管理器需要加上特殊权限:MANAGE_EXTERNAL_STORAGE 文件管理权限
之前申请权限是通过Dialog展现给用户,而Android 11以后通过 Activity 展现给用户看(Intent跳转)。
1)目标sdk修改为30:targetSdkVersion 30
2)清单文件注册:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
3)当系统在11及以上申请此权限
public void checkPermissions() {
//申请危险权限
....
//申请Android11特殊权限
requestManagerPermission();
}
private void requestManagerPermission() {
//当系统在11及以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// 没文件管理权限时申请权限
if (!Environment.isExternalStorageManager()) {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.setData(Uri.parse("package:" + mContext.getPackageName()));
startActivityForResult(intent, REQUEST_MANAGER_PERMISSION);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_MANAGER_PERMISSION && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
//用户拒绝权限,重新申请
if (!Environment.isExternalStorageManager()) {
requestManagerPermission();
}
}
}
二、保存截图失败:open failed: EPERM (Operation not permitted)
前提:发现Android 11 保存截图失败,报错日志:open failed: ENOENT (Operation not permitted)
解决:根据版本,将文件保存在不同的路径下。
默认获取外部存储目录,sdk>29获取外部SD卡缓存目录。
String path = Environment.getExternalStorageDirectory().getPath();
if (Build.VERSION.SDK_INT > 29) {
path = activity.getExternalFilesDir(null).getAbsolutePath() ;
}
File file = new File(path, fileName);
//截图流程...请忽略
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
// 截屏-将view作为原图绘制出来
View v = activity.getWindow().getDecorView();
Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(),Bitmap.Config.RGB_565);
Canvas c = new Canvas(bitmap);
c.translate(-v.getScrollX(), -v.getScrollY());
v.draw(c);
// 压缩Bitmap,不支持png图片的压缩
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
// 把文件插入到系统图库
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
Uri uri =activity.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
// 通知图库更新
activity.sendBroadcast(newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
ToastUtils.showMessage("保存成功");
} catch (IOException e) {
e.printStackTrace();
ToastUtils.showMessage(e.getMessage());
}
参考:https://blog.csdn.net/dongxianfei/article/details/115449709
https://zhuanlan.zhihu.com/p/477539888
边栏推荐
- JSP智能小区物业管理系统
- Review of reflection topics
- DNS攻击详解
- RMAN增量恢复示例(1)-不带未备份的归档日志
- JS to determine whether there is a value in the object in the array
- Oracle 11g sysaux table space full processing and the difference between move and shrink
- ssm人事管理系统
- pm2简单使用和守护进程
- Take you to master the formatter of visual studio code
- Message queue fnd in Oracle EBS_ msg_ pub、fnd_ Application of message in pl/sql
猜你喜欢
随机推荐
sqli-labs通关汇总-page2
Network security -- intrusion detection of emergency response
Pyspark build temporary report error
[leetcode question brushing day 35] 1060 Missing element in ordered array, 1901 Find the peak element, 1380 Lucky number in matrix
Anti shake and throttling of JS
php中树形结构转数组(拉平树结构,保留上下级排序)
Oracle EBs and apex integrated login and principle analysis
PXC high availability cluster summary
Oracle segment advisor, how to deal with row link row migration, reduce high water level
MySQL无order by的排序规则因素
JS to determine whether there is a value in the object in the array
User login function: simple but difficult
Laravel8中的find_in_set、upsert的使用方法
SQLI-LABS通关(less18-less20)
Proteus -- RS-232 dual computer communication
UEditor . Net version arbitrary file upload vulnerability recurrence
MySQL index
Sqli-labs customs clearance (less15-less17)
Only the background of famous universities and factories can programmers have a way out? Netizen: two, big factory background is OK
php中计算树状结构数据中的合计