java - Android 11 : Primary directory (invalid) not allowed for content://media/external/file allowed directories are [Download, 文档]

标签 java android illegalargumentexception android-fileprovider android-11

我正在尝试从 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)

此代码创建一个 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。

关于java - Android 11 : Primary directory (invalid) not allowed for content://media/external/file allowed directories are [Download, 文档],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67367047/

相关文章:

java - 获取异常 : java. lang.IllegalArgumentException:无法添加到布局:约束必须是字符串(或 null)

使用 Java 编译调试时,Java 文件找不到 DeviceEventManagerModule

android - 如何在android中使用ffmpeg合并多个mp3文件

android - IllegalArgumentException:无法在 FileProvider.getUriForFile 上找到包含 xxx 的配置根目录

clojure - Compojure:陷阱 500 URL 解码错误

android - 我可以用蓝牙做到这一点吗?

Java MYSQL,搜索时忽略空白字符串

java.lang.ClassNotFoundException : antlr. ANTLRException 异常

Java 从连续 USB 流读取显示损坏

java - java中的retainAll非常慢......有没有更快的方法?