2012-12-10 30 views
6

Tôi có một WebViewClient đơn giản cho WebView của tôi và trọng shouldInterceptRequest: (Không mã nguồn thật)Android WebClient, trở về một nguồn tài nguyên hình ảnh thông qua WebResourceResponse - không hiển thị hình ảnh

public class WebViewClientBook extends WebViewClient 
{ 
    @Override 
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) 
    { 
     File file = new File("pathToTheFile.jpeg"); 
     FileInputStream inputStream = new FileInputStream(file); 

     return new WebResourceResponse("image/jpeg", "UTF-8", inputStream); 
    } 
} 

Đối với một số lý do, WebClient là không thể để hiển thị hình ảnh ... Tôi tin rằng nó có thể có liên quan đến mã hóa không chính xác: UTF-8.

Bất kỳ đề xuất nào cần sử dụng làm phương án thay thế?

Cảm ơn!

Trả lời

2

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
0

Tôi đã có một vấn đề tương tự và thiết lập hai lá cờ giải quyết vấn đề của tôi:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
    getSettings().setAllowFileAccessFromFileURLs(true); 
    getSettings().setAllowUniversalAccessFromFileURLs(true); 
} 
Các vấn đề liên quan