java.io.FileNotFoundException :(使用ObjectOutputStream写入对象时(权限被拒绝) [英] java.io.FileNotFoundException: (Permission denied) when writing an object using ObjectOutputStream

查看:298
本文介绍了java.io.FileNotFoundException :(使用ObjectOutputStream写入对象时(权限被拒绝)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试解决了几个小时,我非常沮丧,所以我来找你们寻求指导.

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.

我正在尝试保存和检索我创建的User对象.我想要这样做,以便可以从整个应用程序的任何意图中保存和检索此User对象,因此我决定使用FileInput和Output流.我在下面都包含了我的代码.

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.

这是我的输出数据方法:

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;
    }
}

注意:这两个方法都驻留在我的User类中,该类也实现了Serializable.

Note: both of these methods reside inside my User class, which also implements Serializable.

与文件路径有关的整个错误如下所示:java.io.FileNotFoundException: /storage/emulated/0myFile.ser (Permission denied)并出现在以下行:FileOutputStream fos = new FileOutputStream(newFile);当我从不同的意图调用它时,如下所示:User.serializeDataOut(addingUser);其中addingUser是有效的User对象.

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.

在异常日志中看到(权限被拒绝)后,我要做的第一件事是进入清单并检查是否允许我的应用程序对存储进行读写,而实际上我确实这样做了.我在下面包含了我的权限:

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" />

然后我读到一些人,如果他们使用了错误的路径,更确切地说不包括绝对路径,则会遇到此错误,然后我编辑了代码以包含Environment.getExternalStorageDirectory().getAbsolutePath();部分,我很确定自己正在使用正确地.

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.

我还确保,由于我正在仿真器上进行测试,因此我已启用SD卡,并且仿真器具有SD文件夹.我检查了一下,确实确实有一个SD卡文件夹,并且我还在S8上测试了此应用程序,但遇到了同样的错误.

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.

我在这里到底在做什么错?我要做的就是保存一个User对象,然后在其他地方检索它,对于以前的文件被覆盖并且一次只保存一个User的情况,我完全可以.

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.

还有一些可能与我有关的事情:我的Android Monitor中大约每3-5秒,即使我杀死了我的应用程序,错误仍然不断弹出.该错误看起来像这样:onFatalError, processing error from engine(4) com.google.android.apps.gsa.shared.speech.a.g: Error reading from input stream尽管我只能假定这不是问题的根源,但我只是想将其添加进来,以防万一.谢谢

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

推荐答案

您已在清单中添加了权限,所以我确定您不是在询问运行时权限.如果您使用的是SDK 23或更高版本,请询问运行时权限.供参考,我在这里添加一些代码段:

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:

if(Build.VERSION.SDK_INT>22){
     requestPermissions(new String[] {YOUR_PERMISSIONS AS STRING}, 1);
}

,并检查是否已授予权限,您需要使用onRequestPermissionResults()方法.

and to check whether permission is granted or not, you need to use onRequestPermissionResults() method.

@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();
            }
        }
    }
}

这篇关于java.io.FileNotFoundException :(使用ObjectOutputStream写入对象时(权限被拒绝)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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