Android 保存文件权限被拒绝

发布于 2024-12-15 05:42:56 字数 2790 浏览 2 评论 0原文

我想在文件中写入一个位图对象。 这是我的代码:

File f = new File(path + "-bw.jpg");
OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(path + "-bw.jpg"));
image.getBitmap().compress(Bitmap.CompressFormat.JPEG, 100, fileStream);
fileStream.close();

以下是堆栈跟踪:

1-11 09:56:05.553: D/dalvikvm(277): GC_EXTERNAL_ALLOC freed 385 objects / 15832 bytes in 69ms
11-11 09:56:05.733: D/dalvikvm(277): GC_EXTERNAL_ALLOC freed 32 objects / 34048 bytes in 51ms
11-11 09:56:05.923: E/Save error(277): /mnt/sdcard/Water lilies-bw.jpg (Permission denied)
11-11 09:56:05.923: E/Save error(277): java.io.FileNotFoundException: /mnt/sdcard/Water lilies-bw.jpg (Permission denied)
11-11 09:56:05.923: E/Save error(277):  at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
11-11 09:56:05.923: E/Save error(277):  at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
11-11 09:56:05.923: E/Save error(277):  at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
11-11 09:56:05.923: E/Save error(277):  at java.io.FileOutputStream.<init>(FileOutputStream.java:168)
11-11 09:56:05.923: E/Save error(277):  at java.io.FileOutputStream.<init>(FileOutputStream.java:147)
11-11 09:56:05.923: E/Save error(277):  at it.bwpp.activity.ConvertPictureActivity.convertImage(ConvertPictureActivity.java:75)
11-11 09:56:05.923: E/Save error(277):  at it.bwpp.activity.ConvertPictureActivity.access$6(ConvertPictureActivity.java:68)
11-11 09:56:05.923: E/Save error(277):  at it.bwpp.activity.ConvertPictureActivity$1.onClick(ConvertPictureActivity.java:58)
11-11 09:56:05.923: E/Save error(277):  at android.view.View.performClick(View.java:2408)
11-11 09:56:05.923: E/Save error(277):  at android.view.View$PerformClick.run(View.java:8816)
11-11 09:56:05.923: E/Save error(277):  at android.os.Handler.handleCallback(Handler.java:587)
11-11 09:56:05.923: E/Save error(277):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-11 09:56:05.923: E/Save error(277):  at android.os.Looper.loop(Looper.java:123)
11-11 09:56:05.923: E/Save error(277):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-11 09:56:05.923: E/Save error(277):  at java.lang.reflect.Method.invokeNative(Native Method)
11-11 09:56:05.923: E/Save error(277):  at java.lang.reflect.Method.invoke(Method.java:521)
11-11 09:56:05.923: E/Save error(277):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-11 09:56:05.923: E/Save error(277):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-11 09:56:05.923: E/Save error(277):  at dalvik.system.NativeStart.main(Native Method)
11-11 09:56:05.923: I/save image(277): End save image...

错误在哪里?谢谢!

I would like to write a Bitmap Object on file.
This is my code:

File f = new File(path + "-bw.jpg");
OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(path + "-bw.jpg"));
image.getBitmap().compress(Bitmap.CompressFormat.JPEG, 100, fileStream);
fileStream.close();

The following is the stacktrace:

1-11 09:56:05.553: D/dalvikvm(277): GC_EXTERNAL_ALLOC freed 385 objects / 15832 bytes in 69ms
11-11 09:56:05.733: D/dalvikvm(277): GC_EXTERNAL_ALLOC freed 32 objects / 34048 bytes in 51ms
11-11 09:56:05.923: E/Save error(277): /mnt/sdcard/Water lilies-bw.jpg (Permission denied)
11-11 09:56:05.923: E/Save error(277): java.io.FileNotFoundException: /mnt/sdcard/Water lilies-bw.jpg (Permission denied)
11-11 09:56:05.923: E/Save error(277):  at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
11-11 09:56:05.923: E/Save error(277):  at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
11-11 09:56:05.923: E/Save error(277):  at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
11-11 09:56:05.923: E/Save error(277):  at java.io.FileOutputStream.<init>(FileOutputStream.java:168)
11-11 09:56:05.923: E/Save error(277):  at java.io.FileOutputStream.<init>(FileOutputStream.java:147)
11-11 09:56:05.923: E/Save error(277):  at it.bwpp.activity.ConvertPictureActivity.convertImage(ConvertPictureActivity.java:75)
11-11 09:56:05.923: E/Save error(277):  at it.bwpp.activity.ConvertPictureActivity.access$6(ConvertPictureActivity.java:68)
11-11 09:56:05.923: E/Save error(277):  at it.bwpp.activity.ConvertPictureActivity$1.onClick(ConvertPictureActivity.java:58)
11-11 09:56:05.923: E/Save error(277):  at android.view.View.performClick(View.java:2408)
11-11 09:56:05.923: E/Save error(277):  at android.view.View$PerformClick.run(View.java:8816)
11-11 09:56:05.923: E/Save error(277):  at android.os.Handler.handleCallback(Handler.java:587)
11-11 09:56:05.923: E/Save error(277):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-11 09:56:05.923: E/Save error(277):  at android.os.Looper.loop(Looper.java:123)
11-11 09:56:05.923: E/Save error(277):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-11 09:56:05.923: E/Save error(277):  at java.lang.reflect.Method.invokeNative(Native Method)
11-11 09:56:05.923: E/Save error(277):  at java.lang.reflect.Method.invoke(Method.java:521)
11-11 09:56:05.923: E/Save error(277):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-11 09:56:05.923: E/Save error(277):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-11 09:56:05.923: E/Save error(277):  at dalvik.system.NativeStart.main(Native Method)
11-11 09:56:05.923: I/save image(277): End save image...

Where is the error? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迟月 2024-12-22 05:42:56

我认为您忘记向 AnroidManifest.xml 添加 android.permission.WRITE_EXTERNAL_STORAGE 权限。

I think you forgot to add android.permission.WRITE_EXTERNAL_STORAGE permission to you AnroidManifest.xml.

苯莒 2024-12-22 05:42:56

这是一个10年前的问题。
从那时起,情况发生了变化。

现在在清单文件中声明权限是不够的。您需要在运行时请求权限。

即使遵循许可现在也已经过时了。

android.permission.WRITE_EXTERNAL_STORAGE

请参阅此处完整文档

It's a 10-year-old question.
Things have changed since then.

Declaring permissions in the manifest file is not enough now. you need to request permissions at runtime.

Even following permission is obsolete now.

android.permission.WRITE_EXTERNAL_STORAGE

Refer to full documentation here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文