仅提供引用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 stub AssetManager 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); } @Override public boolean onCreate() { return true; } @Override public int delete(Uri uri, String s, String[] as) { throw new UnsupportedOperationException( "Not supported by this provider"); } @Override public String getType(Uri uri) { throw new UnsupportedOperationException( "Not supported by this provider"); } @Override public Uri insert(Uri uri, ContentValues contentvalues) { throw new UnsupportedOperationException( "Not supported by this provider"); } @Override public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) { throw new UnsupportedOperationException( "Not supported by this provider"); } @Override public int update(Uri uri, ContentValues contentvalues, String s, String[] as) { throw new UnsupportedOperationException( "Not supported by this provider"); } }
?
?