打开失败:EACCES(权限被拒绝) [英] open failed: EACCES (Permission denied)

查看:889
本文介绍了打开失败:EACCES(权限被拒绝)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很奇怪的问题,存储访问某些设备上。该应用程序适用于我的测试设备(的Nexus 4和7,三星GS5)。我所有的设备运行Android 4.4.2。不过,我收到了许多电子邮件,来自用户的说,应用程序不能写入存储(无论是内部存储,也没有SD卡)。从日志文件从用户收到的反馈,我能看到的问题是下面的code:

I am having a very weird problem with storage accessing on some devices. The app works on my testing devices (Nexus 4 & 7, Samsung GS5). All my devices running Android 4.4.2. But I received many emails from users saying that the app can not write to the storage (neither the internal storage nor the sd card). From the log file received from user feedback, I can see the problem is the following code:

try {
    if (fStream == null) {
    fStream = new FileOutputStream(filename, true);
}
    fStream.write(data, 0, bytes);
    return;
} catch (IOException ex) {
    ex.printStackTrace();
}

这在该行的fstream =新的FileOutputStream(文件名,真实)抛出异常;创建的FileOutputStream时。

It throws exception at the line fStream = new FileOutputStream(filename, true); when creating FileOutputStream.

该堆栈日志:

W/System.err( 8147): Caused by: java.io.FileNotFoundException: /storage/emulated/0/my_folder/test_file_name.png: open failed: EACCES (Permission denied)
w/System.err( 8147):    at libcore.io.IoBridge.open(IoBridge.java:409)
W/System.err( 8147):    at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
W/System.err( 8147):    at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
W/System.err( 8147):    at myapp.save(SourceFile:515)
W/System.err( 8147):    ... 8 more
W/System.err( 8147): Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
W/System.err( 8147):    at libcore.io.Posix.open(Native Method)
W/System.err( 8147):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
W/System.err( 8147):    at libcore.io.IoBridge.open(IoBridge.java:393)
W/System.err( 8147):    ... 11 more

在AndroidManifest.xml中,我有以下权限宣布:

In the AndroidManifest.xml I have the following permissions declared:

 <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19"/>
    <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

我已经证实,用户正在使用的SD卡上的正确应用程序的私有。而且更重要的是奇怪的是,它没有写入到内部存储。怎么能这样呢,如果我有两个读和放大器;写权限?该用户说,他们不连接的设备连接到PC在那个时候。

I've confirmed that the users are using the correct app's private on the SD card. And what's more weird is that it fails to write to internal storage as well. How can this happen if I have both read & write permissions? The users say they are not connecting their devices to the PC at that time.

更新

原来我打电话开启和关闭的FileOutputStream过于频繁,这将引发FileNotFoundException异常在一些点。听起来更像是一个线程问题。

It turns out I am calling open and close FileOutputStream too frequently, which throws the FileNotFoundException at some point. Sounds more like a threading issue.

推荐答案

我遇到了类似的问题而回。

I ran into a similar issue a while back.

您的问题可能是在两个不同的区域。这是不是你如何创建文件写入,或者说是手机依赖你的写作方法可能是有缺陷的研究。

Your problem could be in two different areas. It's either how you're creating the file to write to, or your method of writing could be flawed in that it is phone dependent.

如果你正在写文件到SD卡上的特定位置,尝试使用环境变量。他们应该始终指向有效的位置。下面是一个例子写入到下载文件夹:

If you're writing the file to a specific location on the SD card, try using Environment variables. They should always point to a valid location. Here's an example to write to the downloads folder:

java.io.File xmlFile = new java.io.File(Environment
    .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
     + "/Filename.xml");

如果你正在写的文件,应用程序的内部存储。试试这个例子:

If you're writing the file to the application's internal storage. Try this example:

java.io.File xmlFile = new java.io.File((getActivity()
   .getApplicationContext().getFileStreamPath("FileName.xml")
   .getPath()));

我个人依靠外部库来处理分流到文件中。这其中并没有让我失望呢。

Personally I rely on external libraries to handle the streaming to file. This one hasn't failed me yet.

org.apache.commons.io.FileUtils.copyInputStreamToFile(is, file);

我已经丢失的数据太多次的失败写命令,让我靠知名和测试库,对我的IO繁重的任务。

I've lost data one too many times on a failed write command, so I rely on well-known and tested libraries for my IO heavy lifting.

还要说明一点,如果文件比较大,你可能也想看看在后台运行的IO,或使用回调。

One more note, if the files are large, you may also want to look into running the IO in the background, or use callbacks.

这篇关于打开失败:EACCES(权限被拒绝)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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