context.getExternalFilesDir() 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上会出现这个问题,那么我们借鉴一下,来改改看。

先查看权限设置:

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

但肯定不是这个原因,代码里使用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要紧(前面的话都是为懒进行开脱)。
改完之后运行代码,完美解决。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
java.io.FileNotFoundException: http://localhost:8080/ 是一个文件找不到的异常。 这个异常通常是由于程序无法在指定的URL中找到文件导致的。例如,在引用提供的代码中,尝试打开一个URL连接来读取该URL指向的文件时,如果该文件不存在,就会抛出这个异常。 要解决这个问题,可以考虑以下几个步骤: 1. 确保URL的正确性: 首先,验证URL是否正确。可以尝试在浏览器中手动访问该URL,看看是否能够成功打开文件。如果无法在浏览器中打开文件,那么很有可能URL指向的文件确实不存在。 2. 检查文件路径: 如果URL是正确的,那么可以检查一下文件路径是否正确。确保文件所在的路径是可以被访问到的,并且文件确实存在于该路径中。 3. 检查文件权限: 如果文件路径是正确的,并且文件确实存在于该路径中,那么可能是由于文件权限的问题导致无法访问该文件。确保程序运行的用户具有足够的权限来访问该文件。 4. 处理异常: 如果以上步骤都确认无误,但仍然无法解决问题,那么可以在程序中使用try-catch语句来捕获FileNotFoundException异常,并进行相应的处理,例如输出错误信息或采取其他措施来处理异常情况。 总结起来,当出现java.io.FileNotFoundException: http://localhost:8080/ 异常时,需要检查URL的正确性、文件路径和文件权限,并处理异常情况。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [解决java java.io.FileNotFoundException: http:\localhost:8080 (文件名、目录名或卷)问题](https://blog.csdn.net/zelda002/article/details/7601653)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [jdbc连接数据库getConnection 增、删、改、查](https://download.csdn.net/download/l_ji_l/3671447)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值