Bạn đang làm nó sai. Bạn có 2 cách để làm điều đó, và nó phụ thuộc vào những gì đang nhận được hình ảnh đó.
Trường hợp 1: Bạn muốn trả lại mảng byte. Trong trường hợp này, bạn nên có một Javascript xử lý nó và phân tích nó thành một chuỗi và gán nó vào trường src của thẻ của bạn trên webView.
File imagefile = new File(otherPath);
FileInputStream fis = null;
try {
fis = new FileInputStream(imagefile);
finall = fis;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bi = BitmapFactory.decodeStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//PNG OR THE FORMAT YOU WANT
bi.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] data = baos.toByteArray();
InputStream is = new ByteArrayInputStream(finaldata);
return new WebResourceResponse("text/html", "UTF-8", is);
Trường hợp 2: Bạn phân tích mọi thứ trên Hoạt động và chuyển mã html đầy đủ để trong webView, bạn sẽ có thuộc tính innerHTML sẽ được cập nhật với dữ liệu này.
File imagefile = new File(otherPath);
FileInputStream fis = null;
try {
fis = new FileInputStream(imagefile);
finall = fis;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bi = BitmapFactory.decodeStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//PNG OR THE FORMAT YOU WANT
bi.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] data = baos.toByteArray();
String image64 = Base64.encodeToString(data, Base64.DEFAULT);
String customHtml = "<html><body><h1>Hello, WebView</h1>" +
"<h2><img src=\"data:image/jpeg;base64," + image64 + "\" /></img></h2></body></html>";
InputStream is = new ByteArrayInputStream(finaldata);
return new WebResourceResponse("text/html", "UTF-8", is);
Trong trường hợp bạn chỉ muốn tải các hình ảnh mà bạn luôn có thể làm một webView.loadData(String data, String mimeType, String encoding)
Hy vọng nó giúp, tôi chỉ bị tôi làm việc với
này