title: Android Download Image and show base64 encoded in webView
body: <p>I want to download images and cache them base64 encoded in database, when i'm online. When my app is offline, i would replace all image tags by the appropriate strings. But if i show them, there are always question mark icons, which e.g. are shown, when an image is not found. (There's no error or warning in LogCat). How could I show the image?</p>
  
  <p>I created a short example app:</p>
  
  <pre><code>@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&lt;String, Void, Boolean&gt;
  {
      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 = "&lt;html&gt;&lt;img src=\"data:image/jpeg;base64," + img + "\" /&gt;&lt;/html&gt;";
          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);
      }
  }
  </code></pre>