I'd like to be able to display images in local directories such as /sdcard/Pictures/ and /sdcard/Download/ in a WebView.
Here is what I've tried:
I have an image file called "levels.png" that I copied from my computer to my device. It is at the "root" of the internal storage that I can access through my computer. This is the /sdcard/ path.
String base = Environment.getExternalStorageDirectory().getAbsolutePath();
String imagePath = "file://" + base + "/levels.png";
String html = "<html><head></head><body><img src=\""+ imagePath + "\"></body></html>";
loadDataWithBaseURL("", html, "text/html","utf-8", "");
loadUrl("file:///"+ Environment.getExternalStorageDirectory().getAbsolutePath() +"/levels.png");
The webpage at file:///storage/emulated/0/levels.png could not be loaded because:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I figured it out. That was way more difficult that it should have been.
So here was the problem:
I was using an Android Marshmallow device as my test device. It seems that the
<uses-permission ../> tag for READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE does not work on Marshmallow. You are required to use the new permissions API to request permissions at runtime.
Check out the correct answer to this question for code:
READ_EXTERNAL_STORAGE permission for Android