Tuesday, February 21, 2012

WebView with zoom controls, image from SDCARD in Android?


Even though, I'm going to load the image file from the filesystem (SDCARD or Phone Storage), instead of Assets folder, I've found a solution for this, and you don't even have to care about ContentProviders... (Which is also a good solution, but this is much easier & quicker in this case.)
The key is WebView's loadDataWithBaseURL method, but let's see an example:

/* Using WebView to display the full-screen image */
    WebView full = (WebView)findViewById(R.id.webview);
    /* Set up the Zoom controls */
    FrameLayout mContentView = (FrameLayout) getWindow().
    getDecorView().findViewById(android.R.id.content);
    final View zoom = this.full.getZoomControls();
    mContentView.addView(zoom, ZOOM_PARAMS);
    zoom.setVisibility(View.VISIBLE);
    /* Create a new Html that contains the full-screen image */
    String html = new String();
    html = ("<html><center&gt;<img src=\""+fileName+"\"></html>" );
    /* Finally, display the content using WebView */
    full.loadDataWithBaseURL("file:///sdcard/data/data/com.youproject.example/",html,"text/html","utf-8","");
Hope you found it useful, let me know how it works for you!

No comments:

Post a Comment