FileNotFoundException打开失败:将图像文件保存到Android上的内部存储期间,EPERM(不允许操作) [英] FileNotFoundException open failed: EPERM (Operation not permitted) during saving image file to internal storage on android

查看:261
本文介绍了FileNotFoundException打开失败:将图像文件保存到Android上的内部存储期间,EPERM(不允许操作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将图像保存到android的内部存储时,我遇到了这个问题.

I faced this problem when i tried to save image to internal storage on android.

public static String setImage(Bitmap image) {
    if (image != null) {
        FileOutputStream outputStream = null;
        File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Caramel");
        dir.mkdir();
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(dir, fileName);
        try {
            outputStream = new FileOutputStream(file);
            image.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            outputStream.flush();
            outputStream.close();
            return fileName;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

一切顺利,我可以在调试模式下看到我的位图图像,但是所有相同,我都会收到下一个错误:

All goes nice and well, and i can see my Bitmap image in debug mode, but all the same i get next error:

    W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Caramel/1587724428205.jpg: open failed: EPERM (Operation not permitted)
W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:495)
W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:235)
W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:186)
W/System.err:     at com.example.caramel.Position.setImage(Position.java:176)
W/System.err:     at com.example.caramel.PositionActivity.onActivityResult(PositionActivity.java:129)
W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:8300)
W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:4905)
W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:4953)
W/System.err:     at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
W/System.err:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
W/System.err:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2043)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
W/System.err:     at android.os.Looper.loop(Looper.java:216)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:7464)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:955)
W/System.err: Caused by: android.system.ErrnoException: open failed: EPERM (Operation not permitted)
W/System.err:     at libcore.io.Linux.open(Native Method)
W/System.err:     at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
W/System.err:     at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
W/System.err:     at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7360)
W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:481)
W/System.err:   ... 17 more

似乎,该原因可能在我的Manifest.xml文件中,但是我已经设置了以下权限:

It seems, that reason could be in my Manifest.xml file, but i've already set these permissions:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

谢谢大家的支持.

推荐答案

作为临时解决方案,您可以执行以下操作:在您的 android/app/src/main/AndroidManifest.xml 文件中,

As a temporary fix you could do: In your android/app/src/main/AndroidManifest.xml file, at

<application android:label="APPNAME" android:icon="ICONNAME" ...>

添加 android:requestLegacyExternalStorage ="true" ,如下所示:

<application android:label="APPNAME" android:icon="ICONNAME" android:requestLegacyExternalStorage="true">

然后它将使用Android 10之前的版本请求访问存储空间,因此您仍然可以像以前那样保存文件.

It will then use the pre-Android 10 way of requesting access to storage, so you can still save your file as you could earlier.

但请注意:将应用更新为目标Android 11(API级别30)后,当您的应用在Android 11设备上运行时,系统会忽略requestLegacyExternalStorage属性,因此您的应用必须准备好支持作用域存储并为这些设备上的用户迁移应用数据.参见 https://developer.android.com/培训/数据存储/使用案例#opt-out-scoped-storage 了解更多信息

But caution: After you update your app to target Android 11 (API level 30), the system ignores the requestLegacyExternalStorage attribute when your app is running on Android 11 devices, so your app must be ready to support scoped storage and to migrate app data for users on those devices. See https://developer.android.com/training/data-storage/use-cases#opt-out-scoped-storage for more info

这篇关于FileNotFoundException打开失败:将图像文件保存到Android上的内部存储期间,EPERM(不允许操作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆