Opening a PDF file using FileProvider uri opens a blank screen

独自空忆成欢 提交于 2019-12-03 12:34:15

I hade the same problem but in my case it was the line:

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

that was missing.

Tried everything for hours and posted here for last resort. After 15 mins, solved it..

For anyone wondering, I changed name attribute of provider from

android:name=".GenericFileProvider"

to

android:name="android.support.v4.content.FileProvider"

I don't even know why I have created a file provider class myself but I remember following a tutorial for it.

Ap11
  File pdfFile = new File(Environment.getExternalStorageDirectory() + "/Ap/" + "manual.pdf");  // -> filename = manual.pdf

    Uri excelPath;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

        excelPath = FileProvider.getUriForFile(mContext, "YourPackageName", pdfFile);
    } else {
        excelPath = Uri.fromFile(pdfFile);
    }
    Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
            pdfIntent.setDataAndType(excelPath, "application/pdf");
            pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            pdfIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            try{
        startActivity(pdfIntent);
    }catch(ActivityNotFoundException e){
        Toast.makeText(mContext, "No Application available to view PDF", Toast.LENGTH_SHORT).show();
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!