gpt4 book ai didi

java - FileNotFoundException 打开失败 : EPERM (Operation not permitted) during saving image file to internal storage on android

转载 作者:行者123 更新时间:2023-12-01 17:13:59 24 4
gpt4 key购买 nike

当我尝试将图像保存到 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;
}

一切都很顺利,我可以在 Debug模式下看到我的位图图像,但仍然出现下一个错误:

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 文件中,但我已经设置了这些权限:

<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 11 中,文件名中不允许使用“:”。当您在文件名末尾附加日期时,它会在末尾添加“:”。所以只需将所有“:”替换为“。”并且工作正常。

        String fileName = 
System.currentTimeMillis().toString().replaceAll(":", ".") + ".jpg";

关于java - FileNotFoundException 打开失败 : EPERM (Operation not permitted) during saving image file to internal storage on android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61406818/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com