java.io.FileNotFoundException: /storage/emulated/0/

今天测试组的同事反馈,apk在应用内部更新时候更新不了。这个问题很严重,记得之前适配androidQ的时候处理过,怎么又出问题了,话不多说,看看日志:

java.io.FileNotFoundException: /storage/emulated/0/myApp/download/myApp.apk: open failed: ENOENT (No such file or directory)

对应到的代码是这一行

 File file = new File(Environment.getExternalStorageDirectory().getPath() + "/myApp/download", "myApp.apk");

            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }

            //异常指向Z这一行
            FileOutputStream fos = new FileOutputStream(file);

FileNotFoundException,顾名思义文件没有找到,网上有很多的帖子都是有说到在androidQ上会出现这个问题,那么我们借鉴一下,来改改看。

先查看权限设置:

 
 

但肯定不是这个原因,代码里使用RxPermission做了动态权限申请的,这个问题在之前适配时处理过的。
查看了自己的compileSdkVersion,targetSdkVersion都配置的是29,并没有什么特别之处,为了排除问题,把原来的代码重新写阅读一下,发现了Environment.getExternalStorageDirectory()不推荐使用了,并给出了一大堆的说明部分提示是这样的:

getExternalStorageDirectory
Added in API level 1
File getExternalStorageDirectory ()
Return the primary shared/external storage directory. This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened. You can determine its current state with getExternalStorageState().
Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.
On devices with multiple users (as described by UserManager), each user has their own isolated shared storage. Applications only have access to the shared storage for the user they're running as.

洋文一大堆,看懂个大概,解决问题要紧吗,既然不推荐使用,那使用可能就会出问题,换了一种替代的写法:

           //创建文件路径
            File dir=new File(context.getExternalFilesDir(null).getPath()+"myApk");
            if (!dir.exists()){
                dir.mkdir();
            }
            //创建文件
            File file = new File(dir+"/"+"jfApp.apk");
            if (!file.exists()){
                file.createNewFile();
            }

            FileOutputStream fos = new FileOutputStream(file);

同时在执行这段之前,检查了SD卡的状态是否可用:

  if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Toast.makeText(mContext, "SD卡不可用~", Toast.LENGTH_SHORT).show();
        } 

还添加了这么一句在Manifest.xml文件中,android:requestLegacyExternalStorage="false"
AndroidQ采用文件分区存储,这句可以把它禁用,这里不深究新的方法,毕竟有这个方案不是很乘数,下个版本要被抛弃了呢,那不白学了,还有,毕竟改bug要紧(前面的话都是为懒进行开脱)。
改完之后运行代码,完美解决。

你可能感兴趣的:(java.io.FileNotFoundException: /storage/emulated/0/)