WebView从数据库异步加载图像 [英] WebView load images asynchronously from Database

查看:237
本文介绍了WebView从数据库异步加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



现在我可以做的只是替换 abc .jpg 部分< img src =abc.jpg> 由 data:image / jpg; base64 ,img_base64_string> ,但这必须在HTML get被呈现之前完成。我想尽快显示HTML页面,然后加载图片。




  • 如何触发加载事件以加载图片


创建一个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]是读者,[1] ]是作者
ParcelFileDescriptor [] pipe = null;

try {
pipe = ParcelFileDescriptor.createPipe();

//通过管道将DB文件复制到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.0encoding =utf-8?> 
< manifest xmlns:android =http://schemas.android.com/apk/res/android>

< uses-sdk android:minSdkVersion =9android: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> p>

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 <img src="abc.jpg"> by data:image/jpg;base64, img_base64_string>, but this has to be done before the HTML get's rendered. I would like to show the HTML page as fast as possible and then load the images.

  • How to trigger a loading event to load an image from the DB?
  • How to correctly set the loaded image in HTML without converting it to base64?

解决方案

The trick is to create a ParcelFileDescriptor.createPipe() to copy the database file through streams to the 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>

To load images in the WebView use the ContentProvider URI + "/image.jpg":
<img src="content://com.example.local.provider/image/29.jpg"></img>

这篇关于WebView从数据库异步加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆