无法在Android中使用意图打开PDF文件

6

我仔细检查了该pdf文件,它在/storage/emulated/0/Download/Abcd.pdf路径下,但是无法使用意图打开。

我在各种查看器中打开它,一些结果显示错误:“无法打开文件”。Microsoft Word提示:检查设备上的文件,但当我在Android文件系统的文件目录中打开Abcd.pdf文件时,它可以正常打开。

我是否设置了错误的路线?

enter image description here

AndroidManifest.xml

<provider
        android:authorities="${applicationId}.provider"
        android:name="android.support.v4.content.FileProvider"
        android:exported="false"
        android:grantUriPermissions="true">

        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

MainActivity.Java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent=new Intent();

    File mypath=new File( Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/","Abcd.pdf");

    Log.e("download",mypath.getAbsolutePath());
      //this log says : /storage/emulated/0/Download/Abcd.pdf
    Uri pdfUri = FileProvider.getUriForFile(getApplicationContext(), getApplicationContext().getPackageName() + ".provider", mypath);



    Log.e("download",mypath.exists()+"");
    if (mypath.exists()) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Log.e("download","result : "+pdfUri.getPath());
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setType("application/pdf");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, pdfUri);
        }else{

        }
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);


        try {
            startActivity(intent);
        }
        catch (ActivityNotFoundException e) {
            Log.e("error","error"+e);
        }
    }
}

res/xml/provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
 <paths xmlns:android="http://schemas.android.com/apk/res/android">
 <external-path
name="storage/emulated/0"
path="."/>
</paths>
2个回答

8
我正在使用这个函数:
public void openPdf(LocalFile magazine) {

        if (BuildConfig.DEBUG)
            Log.d(TAG, "openPdf() called with: magazine = [" + magazine + "]");

        Intent intent = new Intent(Intent.ACTION_VIEW);

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

            File file = new File(Uri.parse(magazine.getPath()).getPath());
            Uri uri = FileProvider.getUriForFile(getContext(),
                getContext().getApplicationContext().getPackageName() + ".provider", file);
            intent.setDataAndType(uri, "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        } else {
            intent.setDataAndType(Uri.parse(magazine.getPath()), "application/pdf");
        }

        try {
            startActivity(intent);
        } catch (Throwable t) {
            t.printStackTrace();
            //attemptInstallViewer();
        }

    }

这对我来说很好用。


2
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 救救我。谢谢。 - W0RT4
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 这几个小时救了我,谢谢您。 - ghiath

1
我有一个问题,通过它可以解决这个问题,你在清单文件中是否编写了存储权限?此外,您的应用程序是否具有读取外部存储器的权限?从移动设置中检查一下,它会自动开始工作。以下是如何在清单文件中初始化权限声明的方法。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

如果在清单文件中给出了权限,且您的手机已经允许该应用获取权限,但应用程序仍无法工作,则请分享活动的完整代码,我可以帮助您解决问题。

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