简体   繁体   English

无法为Intent Android打开pdf文件

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

I thoroughly checked that pdf file is in /storage/emulated/0/Download/Abcd.pdf" but can't open it with intent. 我已彻底检查pdf文件位于/storage/emulated/0/Download/Abcd.pdf"但无法故意打开它。

I opened it in various viewers and some oh them result in a error: "can't open file". 我在各种查看器中打开了它,有的哦,这会导致错误:“无法打开文件”。 Microsoft word says: check file in the device , but Abcd.pdf file is opened well when I opened it in file directory in file system of android. Microsoft word说: check file in the device ,但是当我在android的文件系统中的文件目录中打开Abcd.pdf文件时,它打开的很好。

Did I set the route wrong? 我把路线设置错了吗?

在此处输入图片说明

AndroidManifest.xml 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 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 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>

I am using this function : 我正在使用此功能:

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

    }

This is works fine for me. 这对我来说很好。

I've one question by which this problem might be resolved, Have you wrote Storage Permission in Manifest? 我有一个问题可以解决,您是否在清单中写了存储权限?

Also is your App has Permission to Read External Storage? 另外,您的应用还具有读取外部存储的权限吗? Check this from Mobile Settings and it would start working automatically. 从“移动设置”中进行检查,它将自动开始工作。

Here is how you can initialize Permissions Statement in Manifest File. 这是在清单文件中初始化Permissions语句的方法。

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

If it's given in Manifest and also your Mobile has guaranteed the Permissions and App is not working then share the full code of Activity, I might help you in that. 如果在清单中给出了该密码,并且您的移动设备已确保权限并且应用程序无法正常工作,请共享完整的活动代码,我可能会帮助您。

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

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