Android new File(path).mkdirs() 一直返回false问题,open failed: ENOENT (No such file or directory)

compileSdkVersion: 29版本中

  1. Manifest已经配置权限
  2. 动态申请了权限

出现问题:mkdirs一直返回false

查找问题发现:Environment.getExternalStorageDirectory().getPath(),在compileSdkVersion: 29版本中,该方法标注了@Deprecated,里面已经说明,对于Android Q的手机,应用程序不再可以直接访问此方法返回的路径,所以会一直报错 No such file or directory

解决方式直接上代码
先判断sd卡是否可用,再根据系统设置自己的目录。

    private String environmentFileRoot;//文件根路径
    public String initFileRoot(Context context) {
        if (TextUtils.isEmpty(environmentFileRoot)){
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {//sd卡是否可用
                int currentapiVersion=android.os.Build.VERSION.SDK_INT;//手机系统版本号
                Log.e("FileHelp","SDK_INT::"+currentapiVersion);
                if (currentapiVersion<android.os.Build.VERSION_CODES.Q){
                    environmentFileRoot = Environment.getExternalStorageDirectory().getAbsolutePath();
                }else {
                    File external = context.getExternalFilesDir(null);
                    if (external != null) {
                        environmentFileRoot =  external.getAbsolutePath();
                    }
                }
            }else {
                environmentFileRoot= context.getFilesDir().getAbsolutePath();
            }
        }
        Log.e("FileHelp","environmentFileRoot::"+environmentFileRoot);     
        return environmentFileRoot;
    }
在你的问题中,ENOENT: no such file or directory open 是一个错误提示,意思是没有找到该文件或目录。根据引用的内容,这个错误可能是由于在文件上传功能中未找到指定的文件或目录导致的。为了解决这个问题,你可以按照引用中的建议,检查项目的根目录下是否存在public/upload这个路径,如果没有的话,你可以手动创建这个文件夹。另外,引用指出了一个可能的解决方案,将mkdir() 改成mkdirs(),这样可以创建多层文件夹。通过这些处理,你应该能够解决这个错误并成功进行文件上传。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【已解决】ERROR: ENOENT: no such file or directoryopen](https://blog.csdn.net/m0_51749295/article/details/127860877)[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_1"}}] [.reference_item style="max-width: 50%"] - *3* [FileNotFoundException: open failed: ENOENT (No such file or directory) 问题解决方案](https://download.csdn.net/download/weixin_38685832/14016746)[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_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值