以content://的形式有javascript跨域的问题,所以就算加载js文件成功,也不能执行,或者说还需要解决跨域的问题,太麻烦,所以还是放弃了。我们现在也没有用这种方案。但是加载图片是一点问题都没有的。
希望对同样使用webview开发的兄弟们有所帮助。大家有什么更好的关于webview的开发技术,欢迎分享。
======= ↑↑↑↑↑↑↑↑↑先看上面,可能看完上面的,你就觉得这种方案没必要了 ↑↑↑↑↑↑↑↑↑=================================
继上一文章webview html页面加载本地js及img src
最近又有了新的进展。
首先我们要实现web页加载手机本地的资源(以图片为例)
如content://com.andych008.demo.webview/girl_sd.gif这样的link,我们要在html上显示手机本地的girl_sd.gif这张图片,
至于girl_sd.gif这张图片的位置,依次从以下位置查找,如果找到就打开。
/sdcard/andych008/
/data/data/com.andych008.demo.webview/andych008/
apk:assets/
当然也可以指定只从某个位置打开。
下面是代码实现
public class LocalFileContentProvider extends ContentProvider {
private static final String URI_PREFIX = "content://com.andych008.demo.webview";
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
Log.e("path1:", uri.getPath());
File file = new File(uri.getPath());
ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
return parcel;
}
@Override
public AssetFileDescriptor openAssetFile (Uri uri, String mode) throws FileNotFoundException{
AssetManager am = getContext().getAssets();
String path = uri.getPath().substring(1);
Log.e("path:", path);
//sdcard里有没有
String tpath = "/sdcard/andych008/"+path;
File file = new File(tpath);
if (file.exists()) {
Log.e("path2:", tpath);
Uri turi = Uri.parse(URI_PREFIX+tpath);
return super.openAssetFile(turi, mode);
}
//C盘有没有
tpath = "/data/data/com.andych008.demo.webview/andych008/"+path;
file = new File(tpath);
if (file.exists()) {
Log.e("path2:", tpath);
Uri turi = Uri.parse(URI_PREFIX+tpath);
return super.openAssetFile(turi, mode);
}
try {
AssetFileDescriptor afd = am.openFd(path);
return afd;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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");
}
}
html文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0" />
<title>Insert title here</title>
<link type="text/css" rel="stylesheet" href="1.css">
<script type="text/javascript" charset="utf-8" src="1.js"></script>
</head>
<body>sd这是一个html页面
<hr />
<hr />
<button οnclick="showNote('andy')">showNote</button>
相对路径
<img src="girl_sd.gif" alt="相对路径">
<hr />
sd图片
<img src="content://com.andych008.demo.webview/girl_sd.gif" alt="sd图片">
<hr />
c图片
<img src="content://com.andych008.demo.webview/girl_c.gif" alt="c图片">
<hr />
asset图片
<img src="content://com.andych008.demo.webview/1/girl.gif" alt="a图片">
<hr />
绝对图片
<img src="content://com.andych008.demo.webview/sdcard/andych008/girl_sd.gif" alt="a图片">
</body>
</html>
要求 存在以下文件
/sdcard/andych008/girl_sd.gif
/data/data/com.andych008.demo.webview/andych008/girl_c.gif
apk:assets/1/girl.gif
/data/data下面的文件可以这样cp进去(要求手机是Root过的),或者自己通过程序写文件进去
adb push girl_c.gif "/data/data/com.andych008.demo.webview/andych008/girl_c.gif"
s
s
s