`
destroyed
  • 浏览: 24034 次
  • 来自: ...
最近访客 更多访客>>
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
android手机开发 使用webkit时 远程页面引用本地资源 android手机开发 使用webkit时 远程页面引用本地资源
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");
	}

}
Global site tag (gtag.js) - Google Analytics