Loading individual or Multiple images through assets folder and set into webview to display.
Images can be easily load through assets folder directly using loadUrl(“file:///android_asset/image
_name”); function in webview. so here is the complete step by step tutorial with source code for Load image from assets drawable in webview android.
How to Load image from assets drawable in webview android.
Note: Download and put this image inside assets folder.
Code for MainActivity.java file.
package com.loadimageswebviewdrawable_android_examples.com;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MainActivity extends Activity {
WebView WebViewForm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebViewForm = (WebView)findViewById(R.id.webView1);
WebSettings webSetting = WebViewForm.getSettings();
webSetting.setJavaScriptEnabled(true);
WebViewForm.setWebViewClient(new WebViewClient());
WebViewForm.loadUrl("file:///android_asset/sample_pic.png");
}
private class WebViewClient extends android.webkit.WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return super.shouldOverrideUrlLoading(view, url);
}
}
}
Code for activity_main.xml layout file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.loadimageswebviewdrawable_android_examples.com.MainActivity" > <WebView android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
Screenshot: