Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a local image in webview in android

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

like image 713
brtb Avatar asked Oct 31 '11 16:10

brtb


1 Answers

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 + "\"/>";
like image 95
diegocarloslima Avatar answered Nov 16 '22 05:11

diegocarloslima