如何解决Android 11:主目录无效不允许用于 content://media/external/file 允许目录为 [下载、文档]
我正在尝试从 base64 字符串创建 PDF 文件。由于 Storage Update in Android 11,我必须更改代码,但在 Android 11 设备中出现以下错误:
java.lang.IllegalArgumentException: Primary directory (invalid) not allowed for content://media/external/file; allowed directories are [Download,Documents]
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:172)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:549)
at android.content.ContentResolver.insert(ContentResolver.java:2149)
at android.content.ContentResolver.insert(ContentResolver.java:2111)
public static void createPDF(Context mContext,String fileName,String base64) {
try {
String folderPath;
File dwldsPath;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
folderPath = mContext.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS) + File.separator + "appFolderName";
dwldsPath = new File(folderPath + "/" + fileName);
File folder = new File(folderPath);
folder.mkdirs();
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.disPLAY_NAME,fileName); // file name
values.put(MediaStore.MediaColumns.MIME_TYPE,"application/pdf"); // file extension,will automatically add to file
values.put(MediaStore.DownloadColumns.RELATIVE_PATH,folderPath); // end "/" is not mandatory
Uri uriFile = mContext.getContentResolver().insert(MediaStore.Files.getContentUri("external"),values); // important!
OutputStream outputStream = mContext.getContentResolver().openOutputStream(uriFile);
outputStream.write(Base64.decode(base64,0));
outputStream.close();
} else {
folderPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + File.separator + "appFolderName";
dwldsPath = new File(folderPath + "/" + fileName);
File folder = new File(folderPath);
folder.mkdirs();
FileOutputStream os = new FileOutputStream(dwldsPath,false);
os.write(Base64.decode(base64,0));
os.flush();
os.close();
}
openPDF(mContext,dwldsPath);
} catch (IOException e) {
e.printstacktrace();
} catch (ActivityNotFoundException e) {
Toast.makeText(mContext,"No PDF Viewer Installed",Toast.LENGTH_LONG).show();
}
}
public static void openPDF(Context mContext,File dwldsPath) {
Intent intentUrl = new Intent(Intent.ACTION_VIEW);
Uri uri = FileProvider.getUriForFile(mContext,BuildConfig.APPLICATION_ID + ".provider",dwldsPath);
intentUrl.setDataAndType(uri,"application/pdf");
intentUrl.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentUrl.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
mContext.startActivity(intentUrl);
}
除此错误外,folder.mkdirs() 在 Android 11 中返回 false。这里是 provider_paths.xml 并在 AndroidManifest.xml 中定义
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<files-path
name="files"
path="." />
</paths>
我用谷歌搜索,但找不到任何可行的解决方案来解决问题。提前致谢。
解决方法
我终于找到了解决方案。我不能说这是最好的解决方案,但它运行得很好。
folderPath = mContext.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS) + File.separator + "appFolderName";
到
folderPath = mContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + File.separator + "appFolderName";
我想在 Android 11 中,他们允许在 DOWNLOADS 文件夹中创建文件夹/文件,但 不允许 DOCUMENTS。至少我不能成功。最好的问候。
华为开发者注意事项:您只需保留当前的文件写入策略。现在不需要 Android 11 特殊代码。我尝试了新的文件写入方法,但它崩溃了。显然,他们没有完全实现 Android 11。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。