6

I just want to show a local image with some texts and other stuffs in my webview in android.

i.e i have webview s.t.

 WebView mWebView = (WebView) otherappView.findViewById(R.id.webView1);
 String summary =readRawTextFile(context, R.raw.abc);
 mWebView.loadData(summary, "text/html", null);

and in abc.html file what should i write for src tag of image (???? part)

 <img width="48" height="48" src="??????" class="attachment-48x48 wp-post-image" alt="" title="analogklasik48" />

p.s. the project is a library project so i dont want to use asset folder

p.s. file:///android_res/drawable/image.png doesnt work

Uttam
  • 12,361
  • 3
  • 33
  • 30
brtb
  • 2,201
  • 6
  • 34
  • 53

1 Answers1

1

If you want to put an image from the drawable folder, you should convert it to base64 and then put in your html string:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_drawable);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
String base64Image = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
String imgHtml = "<img src=\"data:image/jpg;base64," + base64Image + "\"/>";
diegocarloslima
  • 1,346
  • 14
  • 16
  • java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference, please review your code with try catch and some explanation – Zhar Dec 30 '19 at 01:06