java.io.FileNotFoundException: (Permission denied) 使用 ObjectOutputStream 写入对象时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44808923/
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
java.io.FileNotFoundException: (Permission denied) when writing an object using ObjectOutputStream
提问by cjnash
I have been trying to work around this for several hours and I am extremely frustrated so I am coming to you guys for some guidance.
我已经尝试解决这个问题好几个小时了,我非常沮丧,所以我来找你们一些指导。
I am trying to save and retrieve a User object I have created. I want to make it so that I can save and retrieve this User object from any intent throughout my application, so I decided to go with a FileInput and Output stream. I have included my code for both below.
我正在尝试保存和检索我创建的用户对象。我想这样做,以便我可以在整个应用程序中从任何意图保存和检索这个 User 对象,所以我决定使用 FileInput 和 Output 流。我在下面包含了我的代码。
Here is my output data method:
这是我的输出数据方法:
public static void serializeDataOut(User ish) {
try {
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
File newFile = new File(path + "myFile.ser");
FileOutputStream fos = new FileOutputStream(newFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(ish);
oos.flush();
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
And here is my input data method:
这是我的输入数据方法:
public static User serializeDataIn(){
try{
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
FileInputStream fin = new FileInputStream(path + "myFile.ser");
ObjectInputStream ois = new ObjectInputStream(fin);
User iUser = (User) ois.readObject();
ois.close();
return iUser;
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
Note: both of these methods reside inside my User class, which also implements Serializable.
注意:这两个方法都驻留在我的 User 类中,该类也实现了 Serializable。
The whole error with the file path looks like this: java.io.FileNotFoundException: /storage/emulated/0myFile.ser (Permission denied)
and it appeared at this line: FileOutputStream fos = new FileOutputStream(newFile);
when I called it from a different intent like so: User.serializeDataOut(addingUser);
in which addingUser was a valid User object.
文件路径的整个错误如下所示:java.io.FileNotFoundException: /storage/emulated/0myFile.ser (Permission denied)
它出现在这一行:FileOutputStream fos = new FileOutputStream(newFile);
当我从不同的意图调用它时,如下所示:User.serializeDataOut(addingUser);
其中 addedUser 是一个有效的 User 对象。
The first thing I did after seeing (Permission denied) in the exception log, was go into my manifest and check if I was allowing my application to read and write to storage, which I did in fact do. I have included my permissions below:
在异常日志中看到(权限被拒绝)后,我做的第一件事是进入我的清单并检查我是否允许我的应用程序读取和写入存储,我确实这样做了。我在下面包含了我的权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Then I read that some people were having this error if they had the wrong path, more specifically not including the absolute path, which I then edited my code to include the Environment.getExternalStorageDirectory().getAbsolutePath();
part, which I am pretty sure I am using correctly.
然后我读到有些人在路径错误时会遇到此错误,更具体地说,不包括绝对路径,然后我编辑了我的代码以包含该Environment.getExternalStorageDirectory().getAbsolutePath();
部分,我很确定我使用正确。
I also made sure that, since I am testing this on an emulator, that I had enabled an SD card and the emulator had an SD folder. I checked and it indeed did have an SD card folder, and I also tested this application on an S8 and I got the same error.
我还确保,因为我是在模拟器上测试的,所以我启用了 SD 卡并且模拟器有一个 SD 文件夹。我检查了一下,它确实有一个 SD 卡文件夹,我还在 S8 上测试了这个应用程序,我得到了同样的错误。
What exactly am I doing wrong here? All I want to do is save one User object and retrieve it somewhere else, and I am perfectly ok with a previous file being overwritten and only having one User saved at a time.
我到底做错了什么?我想要做的就是保存一个 User 对象并在其他地方检索它,我完全可以覆盖之前的文件并且一次只保存一个 User 。
Also something that is probably related I just noticed: about every 3-5 seconds in my Android Monitor, an error keeps on popping up non stop even after I kill my application. The error looks like this: onFatalError, processing error from engine(4)
com.google.android.apps.gsa.shared.speech.a.g: Error reading from input stream
Although I can only assume this isn't the source of the problem, I just wanted to add it in, in case it could help. Thanks
还有一些可能与我刚刚注意到的相关的事情:在我的 Android 监视器中大约每 3-5 秒,即使在我杀死我的应用程序之后,错误也会不停地弹出。错误如下所示:onFatalError, processing error from engine(4)
com.google.android.apps.gsa.shared.speech.a.g: Error reading from input stream
虽然我只能假设这不是问题的根源,但我只是想将其添加进来,以防万一。谢谢
回答by Kaushal28
You've added the permission in manifest, So I'm sure you are not asking runtime permissions. If you are using SDK 23 or higher, Ask runtime permission. For reference I'm adding some snippet here:
您已经在清单中添加了权限,所以我确定您没有要求运行时权限。如果您使用的是 SDK 23 或更高版本,请询问运行时权限。作为参考,我在这里添加了一些片段:
if(Build.VERSION.SDK_INT>22){
requestPermissions(new String[] {YOUR_PERMISSIONS AS STRING}, 1);
}
and to check whether permission is granted or not, you need to use onRequestPermissionResults()
method.
并检查是否授予权限,您需要使用onRequestPermissionResults()
方法。
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
if (!(grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED)) {
Toast.makeText(addAlarm.this, "Permission denied to access your location.", Toast.LENGTH_SHORT).show();
}
}
}
}
回答by P.J.Meisch
This java.io.FileNotFoundException: /storage/emulated/0myFile.ser
looks like you are missing a '/' in the path.
这java.io.FileNotFoundException: /storage/emulated/0myFile.ser
看起来就像你在路径缺少一个“/”。
Use
利用
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + '/';
回答by yanivtwin
Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running
从 Android 6.0(API 级别 23)开始,用户在应用运行时授予应用权限
Everything you need to know is in :
您需要知道的一切都在:
https://developer.android.com/training/permissions/requesting.html
https://developer.android.com/training/permissions/requesting.html
The other error has nothing to do with it
另一个错误与此无关