2013-05-27 110 views
0

我想要下載圖像並將它們緩存在base64編碼數據庫中,當我在線。當我的應用程序脫機時,我會用適當的字符串替換所有圖像標籤。但是如果我給他們看,總會有問號圖標,例如當沒有找到圖像時顯示。 (LogCat中沒有錯誤或警告)。我怎麼能顯示圖像?Android下載圖像並在webView中顯示base64編碼

我創建了一個簡單的例子應用:

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    activity = this; 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    webView = (WebView) findViewById(R.id.webview); 
    webView.setWebViewClient(new MyWebViewClient()); 
    webView.setWebChromeClient(new MyWebChromeClient()); 
    webView.setHttpAuthUsernamePassword(host, "", user, password); 
    new Image().execute(""); 
} 

public String getUrlContent(String urlstring) throws IOException 
{ 
    URL url = new URL(urlstring); 
    URLConnection connection = url.openConnection(); 

    Authenticator.setDefault(new Authenticator() 
    { 
     protected PasswordAuthentication getPasswordAuthentication() 
     { 
      return new PasswordAuthentication(user, password .toCharArray()); 
     } 
    }); 
    HttpURLConnection httpConnection = (HttpURLConnection) connection; 
    httpConnection.setRequestMethod("GET"); 
    httpConnection.connect(); 

    if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) 
    { 
     BufferedReader bufferedReader = new BufferedReader(
       new InputStreamReader(httpConnection.getInputStream())); 
     StringBuilder stringBuilder = new StringBuilder(); 

     String inputLine; 
     while ((inputLine = bufferedReader.readLine()) != null) 
      stringBuilder.append(inputLine + "\n"); 

     return stringBuilder.toString(); 
    } 
    return null; 
} 

private class Image extends AsyncTask<String, Void, Boolean> 
{ 
    private String img; 
    @Override 
    protected Boolean doInBackground(String... string) 
    { 
     try 
     { 
      img = new String(Base64.encodeToString(getUrlContent(url).getBytes(), 
        Base64.DEFAULT)); 
     } catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
     return true; 
    } 

    @Override 
    protected void onPostExecute(Boolean doInBackground) 
    { 
     String html = "<html><img src=\"data:image/jpeg;base64," + img + "\" /></html>"; 
     webView.loadDataWithBaseURL("http://example.com/my.jpg", html, "text/html", null, url); 
     webView.loadData(html, "text/html", "UTF-8"); 
    } 
} 

private class MyWebViewClient extends WebViewClient 
{ 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) 
    { 
     return true; 
    } 

    @Override 
    public void onReceivedError(WebView view, int errorCode, 
      String description, String failingUrl) 
    { 
     Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_LONG) 
       .show(); 
    } 

    @Override 
    public void onReceivedHttpAuthRequest(WebView view, 
      HttpAuthHandler handler, String host, String realm) 
    { 
     handler.proceed(user, password); 
    } 
} 

private class MyWebChromeClient extends WebChromeClient 
{ 
    @Override 
    public void onProgressChanged(WebView view, int progress) 
    { 
     // Activities and WebViews measure progress with different scales. 
     // The progress meter will automatically disappear when we reach 
     // 100% 
     activity.setProgress(progress * 1000); 
    } 

    @Override 
    public void onReachedMaxAppCacheSize(long spaceNeeded, 
      long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) 
    { 
     quotaUpdater.updateQuota(spaceNeeded * 2); 
    } 
} 

回答

1

的方法,下載是不正確的圖像(我想你已經明確地讀取的字節,而不是字符串的圖像)。

這裏是正確的下載代碼:

public String getUrlContent(String urlstring) throws IOException 
{ 
    byte[] imageRaw = null; 
    URL url = new URL(urlstring); 

    Authenticator.setDefault(new Authenticator(){ 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication(user, password.toCharArray()); 
     }}); 
    HttpURLConnection urlConnection = (HttpURLConnection) url 
      .openConnection(); 
    urlConnection.setUseCaches(false); 
    urlConnection.connect(); 
    if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) 
    { 
     try 
     { 
      InputStream in = new BufferedInputStream(
        urlConnection.getInputStream()); 
      ByteArrayOutputStream out = new ByteArrayOutputStream(); 
      int c; 
      while ((c = in.read()) != -1) 
      { 
       out.write(c); 
      } 
      out.flush(); 

      imageRaw = out.toByteArray(); 

      urlConnection.disconnect(); 
      in.close(); 
      out.close(); 
     } catch (IOException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return Base64.encodeToString(imageRaw, Base64.DEFAULT); 
    } 
    return null; 
} 

由於這兩個職位:How to display image with WebView loaddata?How to do HTTP authentication in android?