小编Sab*_*chi的帖子

在Android Nougat中打开时显示空白屏幕的PDF文件

我正在创建一个PDF文件并将其保存在本地存储中.当试图打开它时,它在除了Android N之外的所有设备中都是完美的.我可以使用FileProvider在Android N中打开PDF文件,但它显示为空白.

这是我的URI

content://com.products.provider/external_storage_root/Android/data/com.products/in_17052017_170502_1_1001.pdf
Run Code Online (Sandbox Code Playgroud)

这是我的代码

Uri path;

File pdfFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"
                + "Android" + "/" + "data" + "/" + "com.products" + "/" + file);

if (Build.VERSION.SDK_INT >= 24) {
            path = FileProvider.getUriForFile(getActivity(), "com.products.provider", pdfFile);
        } else {
            path = Uri.fromFile(pdfFile);
        }

        // Setting the intent for pdf reader
        Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
        pdfIntent.setDataAndType(path, "application/pdf");
        pdfIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

        try {
            startActivity(pdfIntent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(getActivity(), "Can't read pdf file", Toast.LENGTH_SHORT).show();
        }
Run Code Online (Sandbox Code Playgroud)

android android-fileprovider android-pdf-api

10
推荐指数
3
解决办法
5708
查看次数