Android 使用FileProvider uri打开PDF文件会打开一个空白屏幕

Android 使用FileProvider uri打开PDF文件会打开一个空白屏幕,android,pdf,android-intent,android-pendingintent,android-fileprovider,Android,Pdf,Android Intent,Android Pendingintent,Android Fileprovider,我正在我的应用程序上创建一个PDF文件,并将其写入外部存储“下载”目录。现在,当我使用FileProvider uri通过我的应用程序以intent action.VIEW打开它时,Google PDF Viewer会显示一个空白屏幕,Adobe Acrobat甚至无法打开该文件。下面是我编写文件并尝试显示它的代码。请注意,我正在发送一个本地通知,并试图通过该通知打开文件 try { File mypath=new File( Environment.g

我正在我的应用程序上创建一个PDF文件,并将其写入外部存储“下载”目录。现在,当我使用FileProvider uri通过我的应用程序以intent action.VIEW打开它时,Google PDF Viewer会显示一个空白屏幕,Adobe Acrobat甚至无法打开该文件。下面是我编写文件并尝试显示它的代码。请注意,我正在发送一个本地通知,并试图通过该通知打开文件

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();
                }
这是我在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>

以下是提供程序路径:

<?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/"/>

请注意,我可以从下载文件夹手动打开保存的文件。我无法通过FileProvider提供的uri打开它


将“exported”(导出)字段更改为true不会改变任何内容。

尝试了数小时的所有操作,并将其张贴在此处作为最后手段。15分钟后,解决了它

对于任何想知道的人,我将提供者的name属性从

android:name=".GenericFileProvider"


我甚至不知道为什么我自己创建了一个文件提供程序类,但我记得我遵循了它的教程

我也遇到了同样的问题,但就我而言,这是一条线:

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
那是丢失的。

对于我的案例-

  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();
    }
/* If set, the new activity is not kept in the history stack.  
 * As soon as the user navigates away from it, the activity is finished. */

 intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

工作了

在这段代码的帮助下,你可以在任何android设备上打开pdf文件。我正试图解决同样的问题。调用getUriForFile时,会发生下一个异常。尝试调用虚拟方法“android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData…”我已将getApplicationContext().getPackageName()替换为您的PackageName。文件存在,我正在检查。已授予权限。我用的是安卓,有可能是我的疼痛吗?android:name=“androidx.core.content.FileProvider你有什么想法吗?android 8.0I已经解决了这个问题。你必须将你的packagename替换为android:authorities,例如“com.example.android.FileProvider”“。我的包名为com.example.root.photolist,但若要写入其名称,则将不正确。如果我指向这个名字,它就不起作用了。您可以在那里详细阅读pdfIntent.addFlags(Intent.FLAG\u ACTIVITY\u NO\u HISTORY);在AndroidX迁移之后,现在是
android:name=“AndroidX.core.content.FileProvider”
/* If set, the new activity is not kept in the history stack.  
 * As soon as the user navigates away from it, the activity is finished. */

 intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);