簡體   English   中英

Android 11:主目錄(無效)不允許內容://媒體/外部/文件允許的目錄是[下載,文檔]

[英]Android 11: Primary directory (invalid) not allowed for content://media/external/file allowed directories are [Download, Documents]

我正在嘗試從 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.Z0F635D0E0F3874FCFF8B581C132ZE6 中定義

<?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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM