android webview 远程 html如何加载本地js 10
html文件放在服务器,js、css放在本地客户端,具体是放在assets目录下,客户端请求的时候加载html页面,但是html页面想使用本地的css、js等资源,如何实...
html文件放在服务器,js、css放在本地客户端,具体是放在assets目录下,客户端请求的时候加载html页面,但是html页面想使用本地的css、js等资源,如何实现呢?
展开
2个回答
展开全部
先把html文件下载到本地,然后更改html中js的路径为本地路径之后进行调用。
1.需要先写一个 LocalFileContentProvider
public class LocalFileContentProvider extends ContentProvider {
public static final String URI_PREFIX = "content://com.youpackage";//这里更改为你的包名
public static String constructUri(String url) {
Uri uri = Uri.parse(url);
return uri.isAbsolute() ? url : URI_PREFIX + url;
}
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
File file = new File(uri.getPath());
ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
return parcel;
}
@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");
}
}
2. 类似 jsUrl为网络端url jsPath为本地路径 data为html文件的字符串内容
data = data.replace(jsUrl, LocalFileContentProvider.URI_PREFIX+jsPath);
mWebView.loadDataWithBaseURL("http://yourwebsite", data, "text/html", "UTF-8", "");
1.需要先写一个 LocalFileContentProvider
public class LocalFileContentProvider extends ContentProvider {
public static final String URI_PREFIX = "content://com.youpackage";//这里更改为你的包名
public static String constructUri(String url) {
Uri uri = Uri.parse(url);
return uri.isAbsolute() ? url : URI_PREFIX + url;
}
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
File file = new File(uri.getPath());
ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
return parcel;
}
@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");
}
}
2. 类似 jsUrl为网络端url jsPath为本地路径 data为html文件的字符串内容
data = data.replace(jsUrl, LocalFileContentProvider.URI_PREFIX+jsPath);
mWebView.loadDataWithBaseURL("http://yourwebsite", data, "text/html", "UTF-8", "");
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询