2012-12-10 9 views
6

Ho un semplice WebViewClient per il mio WebView e sto override shouldInterceptRequest: (non reale il codice)Android WebClient, restituendo una risorsa immagine attraverso WebResourceResponse - non la visualizzazione di immagini

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); 
    } 
} 

Per qualche ragione, il WebClient non è in grado per visualizzare l'immagine ... Credo che potrebbe avere qualcosa a che fare con una codifica errata: UTF-8.

Qualche suggerimento cosa usare in alternativa?

Grazie!

risposta

2

Stai facendo male. Hai 2 modi per farlo, e dipende da cosa sta ricevendo quell'immagine.

Caso 1: si desidera restituire un array di byte. In questo caso, dovresti avere un Javascript che lo tratta e lo analizza in una stringa e assegnalo al campo src del tuo tag sul 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); 

Caso 2: si analizza tutto sulla attività e passare il codice completo HTML così nel WebView si avrà una proprietà innerHTML che verrà aggiornato con questi dati.

 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); 

Nel caso in cui si desidera solo per caricare l'immagine che si può sempre fare un webView.loadData(String data, String mimeType, String encoding)

Speranza che aiuta, ho appena ricevuto il mio lavoro con questo

0

ho avuto un problema simile e impostazione due bandiere risolto i miei problemi:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
    getSettings().setAllowFileAccessFromFileURLs(true); 
    getSettings().setAllowUniversalAccessFromFileURLs(true); 
} 
Problemi correlati