简体   繁体   中英

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.

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.

Did I set the route wrong?

在此处输入图片说明

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>

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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