Opening a PDF file using FileProvider uri opens a blank screen

倾然丶 夕夏残阳落幕 提交于 2019-12-21 04:07:06

问题


I'm creating a PDF file on my app and writing it to the external storage, "Download" directory. Now, when I open it through my app with an intent action.VIEW using FileProvider uri, Google PDF Viewer displays a blank screen, Adobe Acrobat cannot even open the file. Here's my code to write the file and then trying to show it. Note that I'm sending a local notification and trying to open the file through the notification.

try {

                    File mypath=new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),mParam1.getEmlak_id()+".pdf");
                    document.writeTo(new FileOutputStream(mypath));

                    document.close();

                    NotificationManager notificationManager = (NotificationManager)getContext().getSystemService(Context.NOTIFICATION_SERVICE);

                    File file = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),mParam1.getEmlak_id()+".pdf");
                    Uri pdfUri = FileProvider.getUriForFile(getContext(), getContext().getApplicationContext().getPackageName() + ".com.onur.emlakdosyasi.provider", file);
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(pdfUri, "application/pdf");
                    intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

                    //use the flag FLAG_UPDATE_CURRENT to override any notification already there
                    PendingIntent contentIntent = PendingIntent.getActivity(getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

                    Notification notification = new Notification.Builder(getContext())
                            .setContentTitle("title")
                            .setContentText("content")
                            .setSmallIcon(R.drawable.pdficon)
                            .setContentIntent(contentIntent)
                            .setDefaults(Notification.DEFAULT_ALL)
                            .setPriority(Notification.PRIORITY_HIGH)
                            .build();


                    notificationManager.notify(10, notification);

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

Here's my provider on AndroidManifest.xml

<provider
        android:name=".GenericFileProvider"
        android:authorities="${applicationId}.com.onur.emlakdosyasi.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

And here's the provider paths:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
<external-path name="Download" path="Download/"/>

Note that I can open the saved file from the Download folder manually. I just cannot open it through the uri provided by FileProvider.

Changing "exported" field to true doesn't change anything.


回答1:


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

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

that was missing.




回答2:


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.




回答3:


  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();
    }


来源:https://stackoverflow.com/questions/48035736/opening-a-pdf-file-using-fileprovider-uri-opens-a-blank-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!