Sunday, 14 October 2012

Loading an image from base64 string in a webview


public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView wv = (WebView) findViewById(R.id.webView1);
        final String mimeType = "text/html";
        final String encoding = null;

//image64 is the base64 string returned from the server


      String pageData = "<img src=\"data:image/jpeg;base64," + image64 + "\" />";


   
        wv.loadDataWithBaseURL("fake://not/needed", pageData, mimeType, encoding, "");
     
    }
   
    }

And the XML file is


<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" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/textView1"
        android:layout_alignLeft="@+id/button1"
         />

</RelativeLayout>