日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android10 分享微信提示獲取資源失敗

作者:安果移不動 更新時間: 2022-05-12 編程語言

在大于android7的版本下我們要如此設置

  val fileUri = FileProvider.getUriForFile(
            context, context.getPackageName().toString() + ".fileprovider",
            file
        )

但是。。隨著版本迭代。android10來了

把Target升級到30以后。微信分享就報錯了。

需要添加這句話

    val fileUri = FileProvider.getUriForFile(
            context, context.getPackageName().toString() + ".fileprovider",
            file
        )
        context.grantUriPermission(
            "com.tencent.mm",  // 這里填微信包名
            fileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION
        )

完整分享語句

  public boolean shareStream(final File file, final String mimeType) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra(EXTRA_FILEPATH, file.getAbsolutePath());
        intent.setType(mimeType);

        try {
            Uri fileUri = FileProvider.getUriForFile(_context, getFileProviderAuthority(), file);
            // 授權給微信訪問路徑
            AGBase.mContext.grantUriPermission("com.tencent.mm",  // 這里填微信包名
                    fileUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);



            intent.putExtra(Intent.EXTRA_STREAM, fileUri);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            showChooser(intent, null);
            return true;
        } catch (Exception e) { // FileUriExposed(API24) / IllegalArgument
            return false;
        }
    }
    public void showChooser(final Intent intent, final String chooserText) {
        try {
            _context.startActivity(Intent.createChooser(intent, chooserText != null ? chooserText : _chooserTitle));
        } catch (Exception ignored) {
        }
    }

文本文件建議使用 text/plain

iamge 使用別的 image/png? 等

?

原文鏈接:https://blog.csdn.net/mp624183768/article/details/124649928

欄目分類
最近更新