解决open failed: ENOENT (No such file or directory)/(Operation not permitted)

在Android11中,由于新的隐私政策,上传和下载资源需要MANAGE_EXTERNAL_STORAGE权限。申请该权限需通过Activity而不是Dialog。同时,保存截图失败可能是由于EPERM错误,解决方案是根据SDK版本保存文件到不同路径,如外部存储目录或外部SD卡缓存目录,并确保有适当权限。

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());
}


 
 

(venv) D:\qsy\frontend>npm install --unsafe -perm npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open 'D:\qsy\frontend\node_modules\asynckit\LICENSE' npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open 'D:\qsy\frontend\node_modules\asynckit\bench.js' npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open 'D:\qsy\frontend\node_modules\asynckit\index.js' npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open 'D:\qsy\frontend\node_modules\asynckit\stream.js' npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open 'D:\qsy\frontend\node_modules\asynckit\parallel.js' npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open 'D:\qsy\frontend\node_modules\asynckit\serial.js' npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, open 'D:\qsy\frontend\node_modules\asynckit\serialOrdered.js' npm warn cleanup Failed to remove some directories [ npm warn cleanup [ npm warn cleanup '\\\\?\\D:\\qsy\\frontend\\node_modules\\axios', npm warn cleanup [Error: ENOTEMPTY: directory not empty, rmdir 'D:\qsy\frontend\node_modules\axios\lib\core'] { npm warn cleanup errno: -4051, npm warn cleanup code: 'ENOTEMPTY', npm warn cleanup syscall: 'rmdir', npm warn cleanup path: 'D:\\qsy\\frontend\\node_modules\\axios\\lib\\core' npm warn cleanup } npm warn cleanup ], npm warn cleanup [ npm warn cleanup 'D:\\qsy\\frontend\\node_modules\\axios', npm warn cleanup [Error: ENOTEMPTY: directory not empty, rmdir 'D:\qsy\frontend\node_modules\axios\lib\core'] { npm warn cleanup errno: -4051, npm warn cleanup code: 'ENOTEMPTY', npm warn cleanup syscall: 'rmdir', npm warn cleanup path: 'D:\\qsy\\frontend\\node_modules\\axios\\lib\\core' npm warn cleanup } npm warn cleanup ], npm warn cleanup [ npm warn cleanup '\\\\?\\D:\\qsy\\frontend\\node_modules\\@babel\\types', npm warn cleanup [Error: EPERM: operation not permitted, rmdir 'D:\qsy\frontend\node_modules\@babel\types\lib\asserts\generated'] { npm warn cleanup errno: -4048, npm warn cleanup code: 'EPERM', npm warn cleanup syscall: 'rmdir', npm warn cleanup path: 'D:\\qsy\\frontend\\node_modules\\@babel\\types\\lib\\asserts\\generated' npm warn cleanup } npm warn cleanup ], npm warn cleanup [ npm warn cleanup 'D:\\qsy\\frontend\\node_modules\\entities', npm warn cleanup [Error: EPERM: operation not permitted, rmdir 'D:\qsy\frontend\node_modules\entities\lib\esm'] { npm warn cleanup errno: -4048, npm warn cleanup code: 'EPERM', npm warn cleanup syscall: 'rmdir', npm warn cleanup path: 'D:\\qsy\\frontend\\node_modules\\entities\\lib\\esm' npm warn cleanup } npm warn cleanup ] npm warn cleanup ] npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, lstat 'D:\qsy\frontend\node_modules\asynckit\lib' npm warn tar TAR_ENTRY_ERROR ENOENT: no such file or directory, lstat 'D:\qsy\frontend\node_modules\asynckit\lib' npm error code EPERM npm error syscall mkdir npm error path C:\Program Files\nodejs\node_cache npm error errno -4048 npm error Error: EPERM: operation not permitted, mkdir 'C:\Program Files\nodejs\node_cache' npm error at async Object.mkdir (node:internal/fs/promises:852:10) npm error at async makeTmp (C:\Program Files\nodejs\node_modules\npm\node_modules\cacache\lib\content\write.js:156:3) npm error at async handleContent (C:\Program Files\nodejs\node_modules\npm\node_modules\cacache\lib\content\write.js:103:15) { npm error errno: -4048, npm error code: 'EPERM', npm error syscall: 'mkdir', npm error path: 'C:\\Program Files\\nodejs\\node_cache' npm error } npm error npm error The operation was rejected by your operating system. npm error It's possible that the file was already in use (by a text editor or antivirus), npm error or that you lack permissions to access it. npm error npm error If you believe this might be a permissions issue, please double-check the npm error permissions of the file and its containing directories, or try running npm error the command again as root/Administrator. npm notice npm notice New major version of npm available! 10.9.2 -> 11.4.2 npm notice Changelog: https://github.com/npm/cli/releases/tag/v11.4.2 npm notice To update run: npm install -g npm@11.4.2 npm notice npm error Log files were not written due to an error writing to the directory: C:\Program Files\nodejs\node_cache\_logs npm error You can rerun the command with `--loglevel=verbose` to see the logs in your terminal 报错,npm无法安装,帮我解决问题
06-22
strace: Process 5953 attached strace: Process 5954 attached [pid 5954] +++ exited with 0 +++ [pid 5953] --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=5954, si_uid=1000, si_status=0, si_utime=0, si_stime=0} --- strace: Process 5955 attached [pid 5955] socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0) = 8 [pid 5955] connect(8, {sa_family=AF_UNIX, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory) [pid 5955] socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0) = 8 [pid 5955] connect(8, {sa_family=AF_UNIX, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory) [pid 5955] +++ exited with 1 +++ [pid 5953] --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=5955, si_uid=1000, si_status=1, si_utime=0, si_stime=0} --- strace: Process 5956 attached [pid 5956] socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0) = 7 [pid 5956] connect(7, {sa_family=AF_UNIX, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory) [pid 5956] socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0) = 7 [pid 5956] connect(7, {sa_family=AF_UNIX, sun_path="/var/run/nscd/socket"}, 110) = -1 ENOENT (No such file or directory) /usr/bin/fusermount: mount failed: Operation not permitted [pid 5956] +++ exited with 1 +++ [pid 5953] --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=5956, si_uid=1000, si_status=1, si_utime=0, si_stime=0} --- Cannot mount AppImage, please check your FUSE setup. You might still be able to extract the contents of this AppImage if you run it with the --appimage-extract option. See https://github.com/AppImage/AppImageKit/wiki/FUSE for more information [pid 5953] +++ exited with 0 +++ --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=5953, si_uid=1000, si_status=0, si_utime=0, si_stime=0} --- open dir error: No such file or directory +++ exited with 127 +++
最新发布
11-03
出现 'onFailure: content:/media/external/images/media/1000050471: open failed: ENOENT (No such file or directory)' 错误,意味着Glide在尝试加载指定的图片文件时,系统找不到该文件。以下是一些可能的解决办法: #### 1. 检查文件是否存在 需要确认指定的文件在设备上确实存在。可以使用以下代码进行检查: ```java import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.MediaStore; // ... String myImageUrl = "content://media/external/images/media/1000050471"; Uri uri = Uri.parse(myImageUrl); ContentResolver contentResolver = context.getContentResolver(); Cursor cursor = contentResolver.query(uri, null, null, null, null); if (cursor != null && cursor.moveToFirst()) { // 文件存在 cursor.close(); } else { // 文件不存在 if (cursor != null) { cursor.close(); } } ``` #### 2. 检查存储权限 在Android 10及以上版本,需要使用`android:requestLegacyExternalStorage="true"`来启用旧版的外部存储访问模式。在`AndroidManifest.xml`文件中添加如下代码: ```xml <application ... android:requestLegacyExternalStorage="true"> ... </application> ``` 在Android 11及以上版本,需要使用新的存储访问框架(SAF)来访问外部存储。 #### 3. 正确使用`ContentResolver` 如果使用`content://`类型的URI,应该使用`ContentResolver`来访问文件,而不是直接使用`File`对象。示例代码如下: ```java import android.content.ContentResolver; import android.content.Context; import android.net.Uri; import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; import android.widget.ImageView; // ... Context context; ImageView imageView; String myImageUrl = "content://media/external/images/media/1000050471"; Uri uri = Uri.parse(myImageUrl); RequestOptions requestOptions = new RequestOptions(); Glide.with(context) .load(uri) .apply(requestOptions) .into(imageView); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值