java - 如何在android webview中显示图像
问题描述
我正在开发一个 android 项目(java),我想在其中使用 webview 来显示通过使用文件选择器从手机存储中选择的图像。在我的java代码中,我有
package com.example.test;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
Button btn_filePicker;
Intent myFileIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageView);
btn_filePicker = (Button)findViewById(R.id.btn_filePicker);
btn_filePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
myFileIntent.setType("*/*");
startActivityForResult(myFileIntent, 10);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
switch (requestCode) {
case 10:
if (resultCode == RESULT_OK) {
String path = data.getData().getPath();
Uri uri = data.getData();
imageView.setImageURI(uri);
WebView webView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/index.html");
webView.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url){
webView.loadUrl("javascript:init('"+ uri +"')");
}
});
}
break;
}
}
}
这段代码的作用是它获取图像并首先通过图像视图显示它,之后我还希望它以 html 显示图像。在我的html代码中:
<html>
<head>
<title>
Hi
</title>
</head>
<body>
<h1>path</h1>
<img id="image">
<script>
function init(val){
document.write(val);
var img_input = document.getElementById("image");
img_input.src = val;
}
init();
</script>
</body>
</html>
它确实显示了屏幕“content://com.example/document/image:7363364”的路径。但是,它不显示图像。
我的问题是:
有没有办法获取图像并将其添加到 android assets 文件夹中?
如果没有,我该如何解决这个问题?
欢迎任何建议。
解决方案
//Convert it as Bitmap
Bitmap bitmap = null;
String image = null; //Base64 Encoded Image
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
// Convert bitmap to Base64 encoded image for web
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String imgageBase64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
image = "data:image/png;base64," + imgageBase64;
} catch (IOException e) {
e.printStackTrace();
}
String image_final = image;
现在,修改你的onPageFinished
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
if (image_final != null)
webView.loadUrl("javascript:init('" + image_final + "')");
}
});
您的 HTML 代码为:
<html>
<head>
<title>
Hi
</title>
</head>
<body>
<h1>path</h1>
<img id="image">
<script>
function init(val){
document.write(val);
var img_input = document.getElementById("image");
img_input.src = val;
}
// init(); This line is not needed
</script>
</body>
</html>
希望有帮助!
推荐阅读
- python - XML解析-Python
- javascript - 将详细信息按钮添加到动态 HTML 列表
- python - TypeError:“系列”对象不可调用-滚动窗口-Python
- apache-spark - pyspark、jars 和 jupyter 笔记本
- python - 如何使用不同的输入维度执行 KerasClassifier 模型选择
- date - 如何将 DATE 转换为 Salesforce 中的数据类型 DATE
- c# - Math.Round 返回不正确的结果
- javascript - 如何使用 react-native-version-check 打开 App Store 而不是 iTunes Store
- php - 比较单个数组中的每个索引
- java - Json的Flattening和Unflattening是什么意思?我们也可以使用 ObjectMapper 类来反序列化扁平化的 json 属性吗?