当前位置:网站首页>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 07:24:00 【Linxi, Lin Xi】
One 、Android 11 Failed to upload and download resources :open failed: ENOENT (No such file or directory)
Premise : Find out Android 11 Uploading pictures and downloading files failed , Error log :open failed: ENOENT (No such file or directory)
Turned out to be Android 11 Special permissions are required to access the file explorer :MANAGE_EXTERNAL_STORAGE File management permissions
The previous application for permission was through Dialog Show to users , and Android 11 in the future adopt Activity Show the user (Intent Jump ).
1) The goal is sdk It is amended as follows 30:targetSdkVersion 30
2) List file registration :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
3) When the system is 11 And above apply for this permission
public void checkPermissions() {
// Apply for dangerous authority
....
// apply Android11 Special privileges
requestManagerPermission();
}
private void requestManagerPermission() {
// When the system is 11 And above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// Apply for permission without file management permission
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) {
// User denied permission , Reapply
if (!Environment.isExternalStorageManager()) {
requestManagerPermission();
}
}
}
Two 、 Failed to save screenshot :open failed: EPERM (Operation not permitted)
Premise : Find out Android 11 Failed to save screenshot , Error log :open failed: ENOENT (Operation not permitted)
solve : According to the version , Save the file in a different path .
Get external storage directory by default ,sdk>29 Get the outside SD Card cache directory .
String path = Environment.getExternalStorageDirectory().getPath();
if (Build.VERSION.SDK_INT > 29) {
path = activity.getExternalFilesDir(null).getAbsolutePath() ;
}
File file = new File(path, fileName);
// Screenshot process ... Please ignore
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
// screenshots - take view Draw it as the original
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);
// Compress Bitmap, I won't support it png Image compression
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
// Insert the file into the system gallery
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);
// Notify gallery to update
activity.sendBroadcast(newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
ToastUtils.showMessage(" Saved successfully ");
} catch (IOException e) {
e.printStackTrace();
ToastUtils.showMessage(e.getMessage());
}
Reference resources :https://blog.csdn.net/dongxianfei/article/details/115449709
https://zhuanlan.zhihu.com/p/477539888
边栏推荐
- 一个中年程序员学习中国近代史的小结
- Oracle APEX 21.2 installation et déploiement en une seule touche
- [medical] participants to medical ontologies: Content Selection for Clinical Abstract Summarization
- 使用 Compose 实现可见 ScrollBar
- Pratique et réflexion sur l'entrepôt de données hors ligne et le développement Bi
- 读《敏捷整洁之道:回归本源》后感
- One field in thinkphp5 corresponds to multiple fuzzy queries
- JSP智能小区物业管理系统
- Oracle RMAN automatic recovery script (migration of production data to test)
- [introduction to information retrieval] Chapter 1 Boolean retrieval
猜你喜欢

ERNIE1.0 与 ERNIE2.0 论文解读

使用Matlab实现:Jacobi、Gauss-Seidel迭代

spark sql任务性能优化(基础)

读《敏捷整洁之道:回归本源》后感

SSM second hand trading website

Oracle apex Ajax process + dy verification

ssm人事管理系统

Practice and thinking of offline data warehouse and Bi development

@Transitional step pit

MapReduce concepts and cases (Shang Silicon Valley Learning Notes)
随机推荐
Module not found: Error: Can't resolve './$$_ gendir/app/app. module. ngfactory'
User login function: simple but difficult
SSM二手交易网站
DNS攻击详解
Classloader and parental delegation mechanism
TCP攻击
SSM student achievement information management system
@Transitional step pit
view的绘制机制(三)
@Transational踩坑
【MEDICAL】Attend to Medical Ontologies: Content Selection for Clinical Abstractive Summarization
Spark的原理解析
DNS attack details
数仓模型事实表模型设计
A summary of a middle-aged programmer's study of modern Chinese history
架构设计三原则
一个中年程序员学习中国近代史的小结
Take you to master the formatter of visual studio code
Illustration of etcd access in kubernetes
CRP实施方法论