简体   繁体   中英

Problems displaying pdf files on pdf apps using Android 10

I have been trying to display a pdf file choosing some pdf app installed on my Android 10 mobile phone. This is the code I'm using:

Activity

        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/sample.pdf");

                    if(file.exists()) {

                        Uri path = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", file);

                        this.grantUriPermission(this.getPackageName(), path, Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
                        pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                        pdfOpenintent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


                        pdfOpenintent.setDataAndType(path, "application/pdf");


                        try {
                           // ((Activity) ctx).startActivityForResult(pdfOpenintent, 6);
                           this.startActivity(pdfOpenintent);
                        } catch (ActivityNotFoundException e) {

                        }
                    }  else
                        Toast.makeText(this, "No file found" , Toast.LENGTH_LONG).show();

AndroidManifest


    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" />

........


        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>

provider_paths

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

When I choose one of the pdf apps some of them crashes and If I choose Drive pdf Viewer it shows me the name of the file but the screen gets black, so I presume that I'm not opening in the correct way the pdf apps.

As you can see on the Activity I placed several flags, because it seems that new android versions need some kind of permissions to access internal storage.

setFlags() wipes out any previous flags that you have set on the Intent . So, you have three setFlags() calls, and only the last one matters.

Either:

  • Have one setFlags() call, or
  • Use addFlags() instead of setFlags() , at least for the second and subsequent calls

Problem solved. This code should be placed on Manifest according to this link :

<!-- This attribute is "false" by default on apps targeting
     Android 10 or higher. -->
  <application android:requestLegacyExternalStorage="true" ... >
    ...
  </application>

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