Issue
I have the following HTML code loaded into a String
.
<html>
<body>
<img style="width:100%" src="file://storage/emulated/0/sdcard/Download/Page1.jpg"/>
<img style="width:100%" src="file://storage/emulated/0/sdcard/Download/Page2.jpg"/>
<img style="width:100%" src="file://storage/emulated/0/sdcard/Download/Page3.jpg"/>
<img style="width:100%" src="file://storage/emulated/0/sdcard/Download/Page4.jpg"/>
</body>
</html>
And I am trying to load the html into a WebView
in my app as follows.
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadDataWithBaseURL(null, html, "text/html", "utf-8", "");
I have the following permissions in the AndroidManifest.xml
and made sure the permissions are granted.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I went through many answers in SO (e.g. this), but cannot seem to find a solution to load the images from SD Card into my WebView
. I also made sure that the images are not corrupted (I can view those images using the Gallery app). Any thoughts?
I did not find any error in the logcat. However, a possible logcat related to this problem might be the following (I am not sure though).
W/cr_CrashFileManager: /data/user/0/com.example.myapp/cache/WebView/Crash Reports does not exist or is not a directory
Here is how it looks like when the WebView
tries to load the images.
Thanks in advance for your time!
Solution
Instead of using Environment.getExternalStorageDirectory
I used the /sdcard/Downloads
which seems to solve the problem. So the final HTML which I generated that worked is the following.
<html>
<body>
<img style="width:100%" src="file://sdcard/Download/Page1.jpg"/>
<img style="width:100%" src="file://sdcard/Download/Page2.jpg"/>
<img style="width:100%" src="file://sdcard/Download/Page3.jpg"/>
<img style="width:100%" src="file://sdcard/Download/Page4.jpg"/>
</body>
</html>
Answered By - Reaz Murshed
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.