繁体   English   中英

无法为Intent Android打开pdf文件

[英]Can't open pdf file for intent android

我已彻底检查pdf文件位于/storage/emulated/0/Download/Abcd.pdf"但无法故意打开它。

我在各种查看器中打开了它,有的哦,这会导致错误:“无法打开文件”。 Microsoft word说: check file in the device ,但是当我在android的文件系统中的文件目录中打开Abcd.pdf文件时,它打开的很好。

我把路线设置错了吗?

在此处输入图片说明

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>

我正在使用此功能:

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

    }

这对我来说很好。

我有一个问题可以解决,您是否在清单中写了存储权限?

另外,您的应用还具有读取外部存储的权限吗? 从“移动设置”中进行检查,它将自动开始工作。

这是在清单文件中初始化Permissions语句的方法。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

如果在清单中给出了该密码,并且您的移动设备已确保权限并且应用程序无法正常工作,请共享完整的活动代码,我可能会帮助您。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM