我想从本地数据库中异步加载HTML中引用的图像。
现在,我所能做的就是将abc.jpg
的{{1}}部分替换为<img src="abc.jpg">
,但这必须在HTML渲染之前完成。我想尽快显示HTML页面,然后加载图像。
答案 0 :(得分:1)
诀窍是创建一个ParcelFileDescriptor.createPipe()来将数据库文件通过流复制到WebView。
LocalImageContentProvider.java
public class LocalImageContentProvider extends ContentProvider {
private static final String AUTHORITY = "com.example.local.provider";
public static final Uri CONTENT_URI = Uri.parse("content://"+ AUTHORITY +"/image");
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) {
String filename = 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 WebView
new Thread(new PipeRunnable(
getInputStreamFromDbFile(filename),
new AutoCloseOutputStream(pipe[1])) )
.start();
} catch (IOException e) {
e.printStackTrace();
}
return (pipe[0]);
}
}
的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" >
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="19"/>
<application>
<!-- 'activity -->
<provider android:exported="false"
android:name=".provider.LocalImageContentProvider"
android:authorities="com.example.local.provider"
/>
</application>
</manifest>
要在WebView中加载图像,请使用ContentProvider URI +“/ image.jpg”:
<img src="content://com.example.local.provider/image/29.jpg"></img>