简体   繁体   English

FileInputStream 无法读取 Android R 中的文件

[英]FileInputStream can't read file in Android R

File file = new File(filePath);
InputStreamReader is = new InputStreamReader(new FileInputStream(file), charsetName);

simple piece of code.简单的一段代码。 i checked the following:我检查了以下内容:

file!=null
file.isFile()
file.exists()
file.canRead()

everything is true.一切都是真的。 The program have the permission and can actually read this existing file, but put it in a FileInputStream, it throws the following exception:程序有权限并且可以实际读取这个现有文件,但是把它放在一个 FileInputStream 中,它会抛出以下异常:

     java.io.FileNotFoundException: /storage/emulated/0/XNR/Setting.gson: open failed: EFAULT (Bad address)
        at libcore.io.IoBridge.open(IoBridge.java:492)
        at java.io.FileInputStream.<init>(FileInputStream.java:159)
        ...
     Caused by: android.system.ErrnoException: open failed: EFAULT (Bad address)
        at libcore.io.Linux.open(Native Method)
        at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
        at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
        at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
        at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7372)
        at libcore.io.IoBridge.open(IoBridge.java:478)
        at java.io.FileInputStream.<init>(FileInputStream.java:159) 
        ... 

it works well only on a api 23 emulator now.它现在仅在 api 23 仿真器上运行良好。 maybe the issue is cause by some new mechanics of newer version android api?也许问题是由较新版本 android api 的一些新机制引起的?

funny thing is, i developed and tested it on a android r emulator exclusively 2 months ago and everything is fine, after tested on several actuall devices, the project was deemed done and finished, it sat in my hard drive untouched for 2 months, i can even see the same.gson file generated by it.有趣的是,我在 2 个月前专门在 android r 仿真器上开发并测试了它,一切都很好,在几个实际设备上测试后,该项目被认为完成并完成,它在我的硬盘驱动器中放置了 2 个月未动过,我甚至可以看到同样的.gson文件由它生成。 almost looks like it just went bad on it's own.几乎看起来它只是自己变坏了。

Using getFilesDir() or getExternalFilesDir(String) to access storage instead of using Environment.getExternalStorageDirectory() solved the problem, android no longer supports that since q based on the comment above.使用 getFilesDir() 或 getExternalFilesDir(String) 访问存储而不是使用 Environment.getExternalStorageDirectory() 解决了这个问题,android 不再支持,因为 q 基于上面的评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM