compileSdkVersion: 29版本中
- Manifest已经配置权限
- 动态申请了权限
出现问题: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;
}