微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

请在android中建议没有所有文件访问的替代方案

如何解决请在android中建议没有所有文件访问的替代方案

在我的应用程序中,我获得了 PDF 的 base64 字符串数据。我试图直接在应用程序中打开它。但我没有找到任何解决方案。因此尝试了另一种方法,例如,将该数据另存为存储中的 pdf,然后使用外部 PDF 查看器应用程序打开。它工作正常。但它需要所有文件访问权限。现在,在尝试发布 Play 商店时,他们会询问原因或最佳实践的替代解决方案。

请在下面查看我的实现,如果没有使用所有文件访问权限的替代方法,请告诉我。

将 base64 字符串写入文件

String base64= dataPDFString;
            File file5 = null;
            try {
                File pdfDirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
                file5 = new File(pdfDirPath,"MyData" + ".pdf");
                 boolean value1 = true;
                if (!file5.exists()) {

                    value1 = file5.createNewFile();
                }
                if (!checkpermissions()) {

                    requestPermission();
                   }
                else
                {
                    outputStream = new FileOutputStream(file5);
                    outputStream.write(Base64.decode(base64,Base64.NO_WRAP));
                    outputStream.close();

                    if (outputStream != null) {
                        outputStream.close();
                    }

                    OpenFile(file5,base64);

                }

然后在外部应用程序上打开该 PDF 为:

private void OpenFile(File contentUri,String base64) {
    Uri path;
    Intent intent = null;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
       
        path = FileProvider.getUriForFile(getApplicationContext(),getApplicationContext().getPackageName()+".helper.ProviderClass",contentUri);
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(path,"application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(intent);


    }

    else
    {
        path = Uri.fromFile(contentUri);
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(path,"application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivity(intent);
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。