【问题标题】:Opening a PDF file using FileProvider uri opens a blank screen使用 FileProvider uri 打开 PDF 文件会打开一个空白屏幕
【发布时间】:2018-06-10 16:04:21
【问题描述】:

我正在我的应用程序上创建一个 PDF 文件并将其写入外部存储“下载”目录。现在,当我通过我的应用程序通过意图操作打开它时。使用 FileProvider uri 进行查看时,Google PDF 查看器显示一个空白屏幕,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 不会改变任何内容。

【问题讨论】:

    标签: android pdf android-intent android-pendingintent android-fileprovider


    【解决方案1】:

    我也遇到过同样的问题,但在我的情况下是这样的:

    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    

    那是缺失的。

    【讨论】:

    • pdfIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    【解决方案2】:
      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();
        }
    

    【讨论】:

    • 借助此代码,您可以在任何安卓设备中打开 pdf 文件
    • 我正在尝试就同一问题做出决定。当调用 getUriForFile 时发生下一个异常尝试调用虚拟方法 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData...' 我已将 getApplicationContext().getPackageName() 替换为 YourPackageName。文件存在,我正在检查。已授予权限。我正在使用AndroidX,这可能是我的痛苦吗? android:name="androidx.core.content.FileProvider 你有什么想法吗?Android 8.0
    • 我已经解决了这个问题。您必须将 YourPackageName 替换为 android:authorities 中的内容,例如“com.example.android.fileprovider”。我的包被命名为 com.example.root.photolist 但写它的名字是不正确的。如果我指向这个名字,它就不起作用。你可以在那里详细阅读forums.bignerdranch.com/t/…
    【解决方案3】:

    尝试了数小时的所有内容,并在此处发布以备不时之需。 15分钟后,解决了..

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

    android:name=".GenericFileProvider"
    

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

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

    【讨论】:

    • AndroidX迁移后,现在是android:name="androidx.core.content.FileProvider"
    【解决方案4】:

    对于我的情况添加 -

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

    成功了!!!

    【讨论】:

      猜你喜欢
      • 2021-06-12
      • 1970-01-01
      • 1970-01-01
      • 2018-01-28
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      相关资源
      最近更新 更多