Thursday, 20 September 2012

Loading an image stored in assets folder in Webview with static html page in Android




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 = "utf-8";
        final String html = "<h1>Header</h1>" +
        "<p>Custom HTML</p>" +
        "<p> <img height=42 width=42 src = file:///android_asset/ic_launcher.png></img></p>";
        wv.loadDataWithBaseURL("fake://not/needed", html, 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>




Wednesday, 5 September 2012

Converting byte array to long

1.long result = ByteBuffer.wrap(index).getLong();
where index is the byte array



2.  if first byte is the least significant byte
long value1 = 0;
for (int i = 0; i < index.length; i++)
{
  value1 = (value1 << 8) + (index[i] & 0xff);
}


3.if  first byte the most significant
long value = 0;
for (int i = 0; i < index.length; i++)
{
  value += ((long) index[i] & 0xffL) << (8 * i);
}