PDF.js can't show PDF from externalFilesDir at Android 11

1.9k views Asked by At

I am using PDF.js to show PDF files inside my Android App. Up to Android 10 it worked fine, but starting with "targetSdkVersion 30" it doesn't work anymore.

If I want to open and show a PDF file, PDF.js shows

Missing PDF "file:///storage/emulated/0/Android/data/<App-ID>/files/file.pdf

The path ist definitely correct, I checked it and generated it with getExternalFilesDir(null).absolutePath

The whole URL to load is file:///android_asset/web/viewer.html?file=/storage/emulated/0/Android/data/<app-ID>/files/file.pdf

Using "targetSdkVersion 29" and changing nothing else, all works fine, so it seems "targetSdkVersion 30" causes somehow the problem.

1

There are 1 answers

2
Tefracky On

The solution was pretty simple (shame on me):

Up to API 29 I only used:

webView.settings.allowFileAccessFromFileURLs = true

Since API 30 I have to use:

webView.settings.allowFileAccessFromFileURLs = true
webView.settings.allowFileAccess = true