欢迎安卓开发的小伙伴,当你看到此篇文章,如果对你有帮助,请留下你的爱心的小赞,支持一下呦!!!
安卓加载图片的路径
String imageUri = "http://site.com/image.png"; // from Web
String imageUri = "file:///mnt/sdcard/image.png"; // from SD card
String imageUri = "content://media/external/audio/albumart/13"; //
String imageUri = "assets://image.png"; // from assets
String imageUri = "drawable://" + R.drawable.image; // from drawable
安卓加载本地网页
Dialog中显示网页(Webview中加载安卓本地图片)
把网页代码放置在安卓的assets目录下,
通过 String url = "file:///android_asset/xieyi.html";//读取安卓中的网页路径
public class Dialog_web_xieyi extends Dialog {
private WebView webView;
private String title;
public Dialog_web_xieyi(@NonNull Context context, String title) {
super(context);
this.title = title;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//去除标题头
setContentView(R.layout.activity_webview);
webView = (WebView) findViewById(R.id.web_xieyis);
WebSettings webSettings = webView.getSettings();
//允许webview对文件的操作
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);
//用于js调用Android
webSettings.setJavaScriptEnabled(true);
//设置编码方式
webSettings.setDefaultTextEncodingName("utf-8");
webView.setWebChromeClient(new chromClient());
//访问Android assets文件夹内的
String url = "file:///android_asset/xieyi.html";
//访问网页Html
webView.loadUrl(url);
}
private class chromClient extends WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
//页面加载完成执行的操作
String path = "file:///android_asset/imag_xieyi.jpg";
String action = "javascript:aa('" + path + "')";
webView.loadUrl(action);
}
super.onProgressChanged(view, newProgress);
}
}
}
xieyi.html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>Title</title>
</head>
<body>
<img id="img" alt="请查看网络,从新加载" style="width: 100%;height: 100%"/>
<script>
function aa(path){
alert(path);//web弹窗,在手机端也会弹窗,path为窗体内容
var img=document.getElementById("img");
img.src=path;
}
</script>
</body>
</html>
Dialog对话框xml代码
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:id="@+id/web_xieyis"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>