package com.yzq.hybridapp_selectandshowimg.view;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.export.external.interfaces.WebResourceResponse;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.yzq.hybridapp_selectandshowimg.Tool.L;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Map;
/**
* Created by yzq on 2017/7/1.
* <p>
* x5webView 封装 带进度条
*/
public class ProgressWebView extends WebView {
private ProgressBar mProgressBar;
public ProgressWebView(Context context) {
super(context);
initWebView(context);
}
public ProgressWebView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
initWebView(context);
}
public ProgressWebView(Context context, AttributeSet attributeSet, int i) {
super(context, attributeSet, i);
initWebView(context);
}
public ProgressWebView(Context context, AttributeSet attributeSet, int i, boolean b) {
super(context, attributeSet, i, b);
initWebView(context);
}
public ProgressWebView(Context context, AttributeSet attributeSet, int i, Map<String, Object> map, boolean b) {
super(context, attributeSet, i, map, b);
initWebView(context);
}
/*初始化webView*/
private void initWebView(Context context) {
/*添加进度条*/
mProgressBar = new ProgressBar(context, null,
android.R.attr.progressBarStyleHorizontal);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 8);
mProgressBar.setLayoutParams(layoutParams);
mProgressBar.setProgress(0);
addView(mProgressBar);
/*设置Settings*/
final WebSettings webSetting = this.getSettings();
webSetting.setJavaScriptEnabled(true);//开启js脚本
webSetting.setAllowFileAccess(true);//是否允许访问文件
webSetting.setSupportZoom(true);//是否支持缩放
webSetting.setBuiltInZoomControls(true);//是否显示缩放控制栏
/*初始化webClient*/
setWebViewClient(new WebViewClient() {
@Override
public WebResourceResponse shouldInterceptRequest(WebView webView, WebResourceRequest webResourceRequest) {
FileInputStream input;
String url = webResourceRequest.getUrl().toString();
L.i("拦截的url:"+url);
String key = "http://androidimg";
/*如果请求包含约定的字段 说明是要拿本地的图片*/
if (url.contains(key)) {
String imgPath = url.replace(key, "");
L.i("本地图片路径:" + imgPath.trim());
try {
/*重新构造WebResourceResponse 将数据已流的方式传入*/
input = new FileInputStream(new File(imgPath.trim()));
WebResourceResponse response = new WebResourceResponse("image/jpg", "UTF-8", input);
/*返回WebResourceResponse*/
return response;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
return super.shouldInterceptRequest(webView, webResourceRequest);
}
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String s) {
webView.loadUrl(s);
return super.shouldOverrideUrlLoading(webView, s);
}
});
setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView webView, String s) {
super.onReceivedTitle(webView, s);
}
@Override
public void onProgressChanged(WebView webView, int i) {
super.onProgressChanged(webView, i);
if (i == 100) {
mProgressBar.setVisibility(GONE);
} else {
if (mProgressBar.getVisibility() == GONE)
mProgressBar.setVisibility(VISIBLE);
mProgressBar.setProgress(i);
}
}
});
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android WebView 报 Not allowed to load local resource错误的解决办法
共44个文件
png:10个
java:9个
xml:5个
5星 · 超过95%的资源 需积分: 1 140 下载量 102 浏览量
2017-07-28
12:29:05
上传
评论 1
收藏 464KB RAR 举报
温馨提示
Android WebView 报 Not allowed to load local resource错误的解决办法 博客地址:http://blog.csdn.net/yuzhiqiang_1993/article/details/76228541
资源推荐
资源详情
资源评论
收起资源包目录
HybridApp_SelectAndShowImg.rar (44个子文件)
HybridApp_SelectAndShowImg
gradlew 5KB
settings.gradle 16B
HybridApp_SelectAndShowImg.iml 880B
gradle
wrapper
gradle-wrapper.properties 236B
gradle-wrapper.jar 52KB
local.properties 480B
gradlew.bat 2KB
gradle.properties 747B
.gitignore 127B
app
src
androidTest
java
com
yzq
hybridapp_selectandshowimg
ExampleInstrumentedTest.java 808B
test
java
com
yzq
hybridapp_selectandshowimg
ExampleUnitTest.java 428B
main
jniLibs
armeabi
liblbs.so 15KB
AndroidManifest.xml 1KB
res
mipmap-hdpi
ic_launcher.png 3KB
ic_launcher_round.png 4KB
mipmap-xxxhdpi
ic_launcher.png 10KB
ic_launcher_round.png 14KB
mipmap-xhdpi
ic_launcher.png 5KB
ic_launcher_round.png 6KB
mipmap-mdpi
ic_launcher.png 2KB
ic_launcher_round.png 2KB
drawable
layout
activity_main.xml 634B
values
colors.xml 214B
strings.xml 102B
styles.xml 394B
mipmap-xxhdpi
ic_launcher.png 8KB
ic_launcher_round.png 10KB
assets
index.html 405B
js
jquery.min.js 82KB
index.js 423B
css
index.css 203B
java
com
yzq
hybridapp_selectandshowimg
common
Constants.java 1001B
view
ProgressWebView.java 5KB
activity
MainActivity.java 2KB
Tool
MyImagePicker.java 797B
GlideImageLoader.java 2KB
L.java 230B
app
App.java 1KB
libs
tbs_sdk_thirdapp_v3.2.0.1104_43200_sharewithdownload_withfilereader_withoutGame_obfs_20170609_115346.jar 329KB
app.iml 11KB
.gitignore 8B
build.gradle 1KB
proguard-rules.pro 933B
build.gradle 521B
共 44 条
- 1
资源评论
- lqmgf2222019-05-29很好 很好 挺不错的
- liuchaoisgood2018-06-05不错,不错,不错,不错
- jingjdong10232019-08-14(android.app.Activity, java.lang.String, android.widget.ImageView, int, int)' on a null object reference
XeonYu
- 粉丝: 1827
- 资源: 57
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功