Android开发实现webview中img标签加载本地图片的方法
作者:GISuuser 时间:2023-03-15 01:33:40
本文实例讲述了Android开发实现webview中img标签加载本地图片的方法。分享给大家供大家参考,具体如下:
在网上查了很多教程,感觉很麻烦,各种方法,最后实践很简单,主要是两步:
WebSettings webSettings=webView.getSettings();
//允许webview对文件的操作
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);
其次是路径的设置
String path= "file://"+Environment.getExternalStorageDirectory()+ File.separator+"123.jpg";
一定是file:///开头,注意是三个斜杠
下面是结果:
执行代码之前
执行代码之后
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);
var img=document.getElementById("img");
img.src=path;
}
</script>
</body>
</html>
Android代码
package com.example.a5.myapplication;
import android.os.Environment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import java.io.File;
public class MainActivity extends AppCompatActivity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView= (WebView) findViewById(R.id.webView);
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/test.html";
//访问网页Html
// String url="http://192.168.1.121:8080/jsandroid/index.html";
runWebView(url);
}
private class chromClient extends WebChromeClient{
@Override
public void onProgressChanged(WebView view, int newProgress) {
if(newProgress==100){
//页面加载完成执行的操作
String path= "file://"+Environment.getExternalStorageDirectory()+ File.separator+"123.jpg";
String action="javascript:aa('"+path+"')";
new AlertDialog.Builder(MainActivity.this)
.setMessage(action)
.show();
runWebView(action);
}
super.onProgressChanged(view, newProgress);
}
}
private void runWebView(final String url){
runOnUiThread(new Runnable() {
@Override
public void run() {
webView.loadUrl(url);
}
});
}
}
希望本文所述对大家Android程序设计有所帮助。
来源:http://blog.csdn.net/gisuuser/article/details/77721312
标签:Android,webview,本地图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现多线程断点下载
2022-04-27 00:29:16
![](https://img.aspxhome.com/file/2023/9/76319_0s.png)
Android保持屏幕常亮2种实现方法
2022-09-08 11:22:43
Android自定义View实现拖动自动吸边效果
2021-11-18 03:00:38
![](https://img.aspxhome.com/file/2023/3/93273_0s.gif)
浅析Java的Hibernate框架中的缓存和延迟加载机制
2023-04-12 15:58:00
Android编程实现下载图片及在手机中展示的方法
2023-10-26 12:30:59
![](https://img.aspxhome.com/file/2023/8/139158_0s.png)
Kotlin基础教程之数据类型
2023-12-06 15:23:22
![](https://img.aspxhome.com/file/2023/5/65745_0s.png)
IDEA中的maven没有dependencies解决方案
2021-08-01 11:58:50
![](https://img.aspxhome.com/file/2023/1/66641_0s.png)
c#生成验证码程序
2023-01-08 18:38:59
Flutter 分页功能表格控件详细解析
2023-09-22 20:02:45
![](https://img.aspxhome.com/file/2023/2/104692_0s.gif)
Android开发实现判断通知栏是否打开及前往设置页面的方法
2022-10-21 10:43:45
Java中session存储Users对象实现记住密码
2021-06-27 10:46:59
![](https://img.aspxhome.com/file/2023/9/96089_0s.png)
C# partial关键字说明
2021-12-29 15:29:54
C#飞机打字游戏的代码示例(winform版)
2021-09-10 17:43:17
![](https://img.aspxhome.com/file/2023/9/92869_0s.png)
Java数据结构及算法实例:冒泡排序 Bubble Sort
2022-10-17 08:39:45
Java7到Java17之Switch语句进化史示例详解
2021-11-03 18:47:37
Java关键字this与super详解用法
2023-05-28 05:28:04
横竖屏切换导致页面频繁重启screenLayout解析
2021-06-14 05:16:22
![](https://img.aspxhome.com/file/2023/0/137470_0s.png)
java生成图片验证码示例程序
2023-09-13 10:17:57
Java 十大排序算法之冒泡排序刨析
2022-07-05 19:30:29
![](https://img.aspxhome.com/file/2023/4/68134_0s.png)
Intellij IDEA 的maven项目通过Java代码实现Jetty的Http服务器(推荐)
2022-02-19 20:29:13
![](https://img.aspxhome.com/file/2023/2/119172_0s.png)