King of Masses
King of Masses

Reputation: 18765

converting web view as image in Android 4.4 Web-view not working

I have an app that uses webview to display D3 charts. Here is an example of an url that I use for charts: http://nvd3.org/livecode/index.html#codemirrorNav

I am able to load the charts and taken a screenshot of that chart too.. But here the problem with android 4.4 is that it is not possible to capture the webview along with transparent background.

This is my webview

          <WebView
            android:id="@+id/chartView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight=".10"
            android:background="@drawable/chart_bg" >
           </WebView>

I applied the below properties to the web view for loading the charts

    layoutView.getSettings().setJavaScriptEnabled( true );
    layoutView.requestFocusFromTouch();
    layoutView.setBackgroundColor(Color.TRANSPARENT);
    layoutView.getSettings().setBuiltInZoomControls( true );
    layoutView.getSettings().setLoadWithOverviewMode(true);
    layoutView.getSettings().setUseWideViewPort(true);

Here i am converting my webview as image

 private void saveWebView() {

    //Resize the webview to the height of the webpage
    int pageHeight = layoutView.getContentHeight();
    LayoutParams browserParams = layoutView.getLayoutParams();
    layoutView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, pageHeight));

    //Capture the webview as a bitmap
    layoutView.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(layoutView.getDrawingCache());
    layoutView.setDrawingCacheEnabled(false);

    //Create the filename to use
    String randomFilenamepart = String.valueOf(new SecureRandom().nextInt(1000000));
    filename = Environment.getExternalStorageDirectory().toString() + "/Screenshot_" + randomFilenamepart + ".jpg";

    File imageFile = new File(filename);
    //Stream the file out to external storage as a JPEG
    OutputStream fout = null;
    try {
        fout = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
        fout.flush();
        fout.close();

        Log.d("debug", "Screenshot taken successfully");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        layoutView.setLayoutParams(browserParams);
    }
}

By this i am try to capture the web view along with the background below Android 4.3 there's no problem. but On Android 4.4 kitkat, i capture the webview image but it is not giving the background of the webview. Just it is showing the web data with a black screen (not with my background image)

How do I solve that?

Upvotes: 1

Views: 1159

Answers (1)

King of Masses
King of Masses

Reputation: 18765

I solved my problem by an alternative way.. Insted of setting the background image to web view i apply the background image to my D3 chart. And removed the background color also for my web view..

Here is the code:

     <WebView
        android:id="@+id/chartView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight=".10" >
       </WebView>

Web view properties

layoutView.getSettings().setJavaScriptEnabled( true );
layoutView.requestFocusFromTouch();
layoutView.getSettings().setBuiltInZoomControls( true );
layoutView.getSettings().setLoadWithOverviewMode(true);
layoutView.getSettings().setUseWideViewPort(true);

background i am applied through the HTML (chart input)

<style>  body { overflow-y:scroll;  background:url(css/date_input_bg.png) no-repeat; } </style>

Now it working for me.. But still i am amazing why the background and background color both are not working only for Android 4.4 which is working in below!

Upvotes: 1

Related Questions