我正在尝试从 Base64 字符串创建 PDF 文件。由于 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)
此代码创建一个 PDF 文件并将其保存到文件夹中。
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。