Android 异常:java.io.IOException:打开失败:EACCES(权限被拒绝)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21200428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Exception : java.io.IOException: open failed: EACCES (Permission denied)
提问by srsmohit
For some strange reason, am constantly facing an issue with different types of Android devices, for saving the captured images on the device storage.
出于某种奇怪的原因,我经常面临不同类型的 Android 设备的问题,将捕获的图像保存在设备存储上。
Here, is the detailed error log, of what, actually am getting.
这是详细的错误日志,实际上是什么。
java.io.IOException: open failed: EACCES (Permission denied)
at java.io.File.createNewFile(File.java:940)
at com.parkhya.pick_for_shareAflash.HomeActivity.resizeImage(HomeActivity.java:456)
at com.parkhya.pick_for_shareAflash.HomeActivity.onActivityResult(HomeActivity.java:393)
Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
at java.io.File.createNewFile(File.java:933)
Although, all the other Android apps, like, Instagram and others, are able to save the camera clicked images on the devices. Anybody, can you please suggest, what should I do, in order for my app, to save the camera pictures in sdcard.
尽管如此,所有其他 Android 应用程序,如 Instagram 和其他应用程序,都能够将相机点击的图像保存在设备上。任何人,你能建议我应该怎么做,为了我的应用程序,将相机图片保存在 SD 卡中。
回答by Jitesh Upadhyay
please add the permission for Allows an application to write to external storage.
请添加允许应用程序写入外部存储的权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Allows an application to read from external storage.
允许应用程序从外部存储读取。
Any app that declares the WRITE_EXTERNAL_STORAGE permission is implicitly granted this permission.
任何声明 WRITE_EXTERNAL_STORAGE 权限的应用程序都被隐式授予此权限。
This permission is enforced starting in API level 19. Before API level 19, this permission is not enforced and all apps still have access to read from external storage. You can test your app with the permission enforced by enabling Protect USB storage under Developer options in the Settings app on a device running Android 4.1 or higher.
此权限从 API 级别 19 开始强制执行。在 API 级别 19 之前,不强制执行此权限,所有应用程序仍然有权从外部存储读取。您可以通过在运行 Android 4.1 或更高版本的设备上的“设置”应用程序中的“开发人员选项”下启用“保护 USB 存储”来使用强制执行的权限测试您的应用程序。
回答by Smeet
This may help you. I face the same issue when writing the file on sdcard. I have set all required permission to write the file but I used the file object like below:
这可能对你有帮助。在 sdcard 上写入文件时,我遇到了同样的问题。我已经设置了写入文件所需的所有权限,但我使用了如下所示的文件对象:
Wrong:
错误的:
File myFile = new File(Environment.getExternalStorageDirectory().getAbsoluteFile()+fileName);
Correct:
正确的:
File myFile = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), fileName);
That means the path was wrong.
那就是路径不对。
回答by madmaskd
Solution what i find is
我发现的解决方案是
edit the Emulator 1. go to android virtual device manager and then edit the emulator 2. set say 100 MB for Sd card for the respected emulator and say Ok 3. save and close emulator and start 4. path saved is click DDMS mnt/sdcard/yourfilename it worked for me the app is not giving Error and is working
编辑模拟器 1. 转到 android 虚拟设备管理器,然后编辑模拟器 2. 为受人尊敬的模拟器设置 100 MB 的 Sd 卡并说 Ok 3. 保存并关闭模拟器并启动 4. 保存的路径是单击 DDMS mnt/sdcard /yourfilename 它对我有用 该应用程序没有给出错误并且正在工作