Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView load from internal storage | Android 11 | Android R

Tags:

android

Actually my requirement is I want to load dynamic files into WebPage like image. video, audio etc.., it's either from asset or apps own files directory, that's not a problem.

I tried using following two ways.

Way 1

Here is my html file is in assets folder, and I have 123.jpg in both assets folder and also internal folder,

Home.html,

<!DOCTYPE html>
<html>
   <head>
      <title>Page Title</title>
   </head>
   <body>
      <h1>Image testing</h1>
      <p>Relative Path</p>
      <img src="123.jpg" alt="Italian Trulli" width="100%">
      <p>From Files Directory</p>
      <img src="file:////data/user/0/com.guna.testapplication/files/123.jpg" alt="Italian Trulli" width="100%">
   </body>
</html>

And I am loading it into webview like

webView.loadUrl("file:///android_asset/Home.html")

And here is my output for 29,

enter image description here

Look, both from Asset and files directory loaded as expected.

And here is for 30,

enter image description here

Here only from asset directory loaded. And files directory not loaded.

Way 2

Here I'm loading both html and image from internal storage.

Home.html

<!DOCTYPE html>
<html>
   <head>
      <title>Page Title</title>
   </head>
   <body>
      <h1>Image testing</h1>
      <img src="123.jpg" alt="Italian Trulli" width="100%">
   </body>
</html>

And I'm loading webview like this,

webView.loadUrl("file:////data/user/0/com.guna.testapplication/files/Home.html")

If my compileSdkVersion 29 and also targetSdkVersion 29 this works well,

Output for 29

enter image description here

If I change SdkVersion like compileSdkVersion 30 and also targetSdkVersion 30 this gives me access denied error,

Output on 30

enter image description here

Manifest

<uses-permission android:name="android.permission.INTERNET" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:usesCleartextTraffic="true">
    <activity
        android:name=".MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
like image 493
Gunaseelan Avatar asked Sep 10 '20 13:09

Gunaseelan


People also ask

What is Android system WebView and how to use it?

Developers can use Android System WebView to add browser functionality to the apps they develop, by including the WebView code library in their software. By including this, they can effectively insert a browser within their apps, so that you can use these apps to interact with web pages and web-based apps.

What is internal storage in Android?

Important Points About Internal Storage In Android: The stored data in memory is allowed to read and write files. When files are stored in internal storage these file can only be accessed by the application itself not by other applications.

Where are Android app internal data files saved?

All android app internal data files is saved in /data/data/<your app package name> folder like below. In this example, my app data internal file is saved in /data/data/com.dev2qa.example folder.

How to read media files from external storage on Android?

To read the media file use these permissions: READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE for Android 10 or higher. c. You can access the media files by other files through READ_EXTERNAL_STORAGE and the document files, through system file picker. d. If you uninstall the app, the data would not be deleted from the External Storage. 3.


1 Answers

Found the Answer,

From setAllowFileAccess we have to explicitly set it to true when we are targeting Build.VERSION_CODES.R, otherwise it wont allow you to load file:// URLs.

So the solution is

webView.settings.allowFileAccess = true

In Java,

webView.getSettings().setAllowFileAccess(true);

This works as expected for both scenario.

like image 141
Gunaseelan Avatar answered Oct 16 '22 15:10

Gunaseelan