android - 即使授予权限后,也无法在 SDK30 中使用以下代码创建文件
问题描述
FileOutputStream outputStream = null;
File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
//String string = Environment.getExternalStorageDirectory().toString();
File dir = new File(file + "/MyPics");
dir.mkdirs();
String filename = String.format("%d.jpg",System.currentTimeMillis());
Log.e(TAG," saveToGallery filename" + filename);
File outFile = new File(dir,filename);
Log.e(TAG," saveToGallery outFile " + outFile.toString() + " Output file exists : " + outFile.exists());
if (outFile.exists ()) {
outFile.delete();
Log.e(TAG," saveToGallery outFile.delete() " );
}
if(outFile.createNewFile()) {
Log.e(TAG, " saveToGallery outFile.createNewFile() " + outFile.toString() + " Output file exists : " + outFile.exists());
try {
outputStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
Log.e(TAG, " saveToGallery outputStream " + outputStream.toString());
} catch (Exception e) {
Log.e(TAG, " saveToGallery Creating outputStream failed ");
e.printStackTrace();
}
错误日志:
2020-11-04 05:07:38.479 24501-24501/com.mtech.mtechproject E/ checkPermission Function: Granted permission for writing: 101
2020-11-04 05:07:38.479 24501-24501/com.mtech.mtechproject E/BaseActivity: saveToGallery ++
2020-11-04 05:07:38.486 24501-24501/com.mtech.mtechproject E/BaseActivity: saveToGallery filename1604446658485.jpg
2020-11-04 05:07:38.488 24501-24501/com.mtech.mtechproject E/BaseActivity: saveToGallery outFile /storage/emulated/0/Pictures/MyPics/1604446658485.jpg Output file exists : false
2020-11-04 05:07:38.490 24501-24501/com.mtech.mtechproject W/System.err: java.io.IOException: No such file or directory
2020-11-04 05:07:38.491 24501-24501/com.mtech.mtechproject W/System.err: at java.io.UnixFileSystem.createFileExclusively0(Native Method)
2020-11-04 05:07:38.491 24501-24501/com.mtech.mtechproject W/System.err: at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:317)
2020-11-04 05:07:38.491 24501-24501/com.mtech.mtechproject W/System.err: at java.io.File.createNewFile(File.java:1008)
2020-11-04 05:07:38.491 24501-24501/com.mtech.mtechproject W/System.err: at com.mtech.mtechproject.BaseActivity.saveToGallery(BaseActivity.java:306)
2020-11-04 05:07:38.491 24501-24501/com.mtech.mtechproject W/System.err: at com.mtech.mtechproject.BaseActivity.checkPermission(BaseActivity.java:266)
解决方案
使用以下代码检查您的版本是否 >= Android 11,如果使用 getExternalFilesDir() 标志,getExternalStorageDirectory() 将不起作用。在下面的函数中,布尔值将返回文件是否存在于外部存储中。
public boolean checkImageInLocalStorage(String fileName, String folderName, String folderType){
boolean isPresent =false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
File rootPath= context.getExternalFilesDir("AppFolderName").getAbsoluteFile();
String rootFolder =Constants.DOWNLOAD_DIRECTORY_NAME;
File localFile = new File(rootPath+"/"+rootFolder+"/"+folderType+"/"+folderName+"/"+fileName);
if (localFile.exists() && !localFile.isDirectory()){
isPresent = true;
}
}else {
File rootPath = Environment.getExternalStorageDirectory();
String root = rootPath.getAbsolutePath().toString();
String rootFolder = Constants.DOWNLOAD_DIRECTORY_NAME;
// File rootPath=context.getFilesDir();
File localFile = new File(rootPath.getAbsoluteFile().getAbsolutePath());
localFile = new File(root + "/" + rootFolder + "/" + folderType + "/" + folderName + "/" + fileName);
if (localFile.exists() && !localFile.isDirectory()) {
// do something
isPresent = true;
}
}
return isPresent;
}
推荐阅读
- java - 是否可以为 JAR 文件运行 Junit 测试
- python - 密码学构建轮子失败。无法为使用 PEP 517 且无法直接安装的密码学构建轮子
- c++ - VS2019:致命错误 C1083 无法打开头文件:'opencv.hpp'
- javascript - 没有得到数组的值,但数组存在 - JS
- mysql - 如何在 Airflow MySQLHook 中支持 SSL(特别是 AWS 2019CA)
- npm - PNPM 安装确实有效,但 ProdBuild 失败
- java - MYSQL Left join 失败,java 中出现“未知列”
- zapier - 同时添加新人和新交易
- apache-kafka - 来自不同消费者组的多个消费者如何从同一个分区中读取数据?
- c# - 为一群人提供可见性 Web 选项卡,从数据库获取数据到布局页面 | ASP.NET 核心 3.1