Webview Load Images Asynchronously From Database
I would like to load images referenced in HTML asynchronously from my local Database. Right now all I can do is to replace the abc.jpg part of
by data:ima

Solution 1:
The trick is to create a ParcelFileDescriptor.createPipe() to copy the database file through streams to the WebView.
LocalImageContentProvider.java
publicclassLocalImageContentProviderextendsContentProvider {
privatestaticfinalStringAUTHORITY="com.example.local.provider";
publicstaticfinalUriCONTENT_URI= Uri.parse("content://"+ AUTHORITY +"/image");
@Overridepublic ParcelFileDescriptor openFile(Uri uri, String mode) {
Stringfilename= uri.getLastPathSegment();
// http://developer.android.com/reference/android/os/ParcelFileDescriptor.html#createPipe%28%29 // [0] is the reader, [1] is the writer
ParcelFileDescriptor[] pipe = null;
try {
pipe = ParcelFileDescriptor.createPipe();
// copy DB file through pipe to WebViewnewThread(newPipeRunnable(
getInputStreamFromDbFile(filename),
newAutoCloseOutputStream(pipe[1])) )
.start();
} catch (IOException e) {
e.printStackTrace();
}
return (pipe[0]);
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android" ><uses-sdkandroid:minSdkVersion="9"android:targetSdkVersion="19"/><application><!-- 'activity --><providerandroid:exported="false"android:name=".provider.LocalImageContentProvider"android:authorities="com.example.local.provider"
/></application></manifest>
To load images in the WebView use the ContentProvider URI + "/image.jpg":
<img src="content://com.example.local.provider/image/29.jpg"></img>
Post a Comment for "Webview Load Images Asynchronously From Database"