读书人

android手机开发 施用webkit时 远程页

发布时间: 2012-08-27 21:21:56 作者: rapoo

android手机开发 使用webkit时 远程页面引用本地资源

仅提供引用android工程中assets目录下资源的方法。

如果要引用sdcard上的资源,代码中也给出了参考。自己改

?

主要就是提供一个ContentProvider,并修改在页面中的引用为content://你的URI前缀/实际的地址

如:

<img src="content://com.xxxxx.localfile/icon.png"/>

?

贴代码吧

package com.xxxxxxx;import java.io.FileNotFoundException;import java.io.IOException;import android.content.ContentProvider;import android.content.ContentValues;import android.content.res.AssetFileDescriptor;import android.content.res.AssetManager;import android.database.Cursor;import android.net.Uri;/** * 加载本地文件的内容提供者 *  * @author a7 *  */public class LocalFileContentProvider extends ContentProvider {private static final String URI_PREFIX = "content://com.xxxxxx.localfile";public static String constructUri(String url) {Uri uri = Uri.parse(url);return uri.isAbsolute() ? url : URI_PREFIX + url;}@Override/** * 直接读取程序中的资源文件<br> * 取sd卡文件实现openfile方法即可,需要用到ParcelFileDescriptor  *  */public AssetFileDescriptor openAssetFile(Uri uri, String mode)throws FileNotFoundException {// TODO Auto-generated method stubAssetManager am = getContext().getAssets();String path = uri.getPath().substring(1);try {AssetFileDescriptor afd = am.openFd(path);return afd;} catch (IOException e) {}return super.openAssetFile(uri, mode);}@Overridepublic boolean onCreate() {return true;}@Overridepublic int delete(Uri uri, String s, String[] as) {throw new UnsupportedOperationException("Not supported by this provider");}@Overridepublic String getType(Uri uri) {throw new UnsupportedOperationException("Not supported by this provider");}@Overridepublic Uri insert(Uri uri, ContentValues contentvalues) {throw new UnsupportedOperationException("Not supported by this provider");}@Overridepublic Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) {throw new UnsupportedOperationException("Not supported by this provider");}@Overridepublic int update(Uri uri, ContentValues contentvalues, String s,String[] as) {throw new UnsupportedOperationException("Not supported by this provider");}}

?

?

读书人网 >Web前端

热点推荐