Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 474
Tefracky Avatar asked Oct 06 '20 09:10

Tefracky


People also ask

Why can't I open PDF files on my Android phone despite having PDF readers installed?

To fix a PDF file not opening in Adobe reader, you will need to download the latest version of Adobe Reader. After which you will disable the protected mode that comes with it by default. Once this is changed, the issue of the PDF file not opening in Adobe reader will be resolved.

Does PDF JS work on Android?

js by Manuel Reimer. Opens PDF files on Firefox for Android using PDF. js.

How can I open PDF file in Android emulator?

To launch an Intent on a PDF file, the mobile device should have installed a program that can read PDF documents. If you do not want the user to have a PDF reader's installed, you must modify your application to be able to read PDF documents. Show activity on this post. you need to install apk in emulator.

Why can my phone not display PDF?

Why can't I open PDF files on my Android phone? If you can't view PDF documents on your device, check if the file is corrupted or encrypted. If that isn't the case, use different reader apps, and see which one works for you.


Video Answer


1 Answers

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
like image 149
Tefracky Avatar answered Sep 29 '22 06:09

Tefracky