使用FileProvider URI打开PDF文件时出现空白屏幕

17

我正在我的应用程序中创建一个PDF文件,并将其写入外部存储器的“下载”目录。现在,当我使用FileProvider uri通过我的应用程序打开它,并使用intent动作.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并不会改变任何内容。

4个回答

11

我遇到了同样的问题,但在我的情况下是这行代码:

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

那是失踪了的。


1
pdfIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); - Vinit ...

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

通过这段代码,您可以在任何Android设备上打开pdf文件。 - Ap11
我正在尝试决定相同的问题。 当调用getUriForFile时,下一个异常会发生 尝试调用虚拟方法'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData...' 我已经将getApplicationContext().getPackageName()替换为YourPackageName。文件存在并且我正在检查它。权限已被授予。我正在使用AndroidX,这可能是我的痛苦吗? android:name="androidx.core.content.FileProvider 你有什么想法? Android 8.0 - Orlov Const
我已经解决了这个问题。你需要将“YourPackageName”替换为在android:authorities中放置的内容,例如“com.example.android.fileprovider”。我的包名为com.example.root.photolist,但写它的名称是不正确的。如果我指向这个名称,它就不起作用。你可以在这里详细阅读https://forums.bignerdranch.com/t/solved-error-at-taking-picture-with-intents-need-serious-help/13900 - Orlov Const

5

花了数小时尝试各种方法,最后才在这里寻求帮助。15分钟后,问题得到解决...

如果有人感兴趣,我将提供者(provider)的名称属性(name attribute)更改为:

android:name=".GenericFileProvider"

to

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

我甚至不知道为什么要自己创建一个文件提供程序类,但我记得是在跟随教程操作。


2
AndroidX迁移后,现在是android:name="androidx.core.content.FileProvider" - 6rchid

0

对于我的情况,添加-

/* 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);

工作了!!!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接