Loading...

android-developers@googlegroups.com

[Prev] Thread [Next]  |  [Prev] Date [Next]

[android-developers] Re: How to get webview's content into a bitmap? gjs Thu Sep 10 09:01:01 2009

Hi again,

I thought you pasted the correct code already ?

Anyway for risk of being accused of writing the code for you, the
following will write a jpg file, to the sdcard, which contains the
entire content of the web page,

when I test it on a G1 phone in portrait mode I get an image of the
yahoo page that is currently 315 x 1476 pixels -

//////////////////////////////////////////////////////////////////////

package com.testWebView;

import java.io.FileOutputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Picture;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class testWebView extends Activity
{
        WebView w = null;

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        w = new WebView(this);

        w.setWebViewClient(new WebViewClient()
        {
                public void onPageFinished(WebView view, String url)
                {
                        Picture picture = view.capturePicture();

                Bitmap  b = Bitmap.createBitmap( picture.getWidth(),
picture.getHeight(), Bitmap.Config.ARGB_8888);

                Canvas c = new Canvas( b );

                picture.draw( c );

                FileOutputStream fos = null;

                try {

                        fos = new FileOutputStream( "/sdcard/yahoo_" +
System.currentTimeMillis() + ".jpg" );

                        if ( fos != null )
                        {
                                b.compress(Bitmap.CompressFormat.JPEG, 90, fos 
);

                                fos.close();
                        }

                } catch( Exception e )
                        {
                        //...
                        }
                }
          });

        setContentView( w );

        w.loadUrl( "http://www.yahoo.com");

    }

}

//////////////////////////////////////////

and the AndroidManifest.xml -

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.testWebView"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/
app_name">
        <activity android:name=".testWebView"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category
android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.INTERNET"></uses-
permission>
</manifest>

//////////////////////////////////////////////////////////

Now you can stop asking the same question in the discuss group  as
well !

http://groups.google.com/group/android-discuss/browse_thread/thread/0c7891cac8225505/8bde5eab18ef05c0#8bde5eab18ef05c0

Regards

On Sep 10, 1:33 pm, AJ <[EMAIL PROTECTED]> wrote:
> If somebody knows this, plz help me
>
> - AJ
>
> On Sep 9, 1:26 pm, Ajeet Singh <[EMAIL PROTECTED]> wrote:
>
> > Hi gjs,
>
> > Thanks for pointing that error. But that was a type error. Now I am
> > pasting the correct code here.
>
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> >        WebVieww = newWebView(this);
> >         w.loadUrl("http://www.yahoo.com");
> >        BitmapmBitmap          = null;
> >         ByteArrayOutputStream mByteArrayOpStream        = null;
>
> >         //get the picture from thewebview
> >         Picture picture = w.capturePicture();
>
> >         //Create the new Canvas
> >         Canvas mCanvas = new Canvas();
>
> >         //Copy the view canvas to abitmap
> >         try{
> >                 //w.draw(mCanvas);
> >                 //mCanvas.save();
> >                 //picture.draw(mCanvas);
> >                 mCanvas.drawPicture(picture);
> >                 //int restoreToCount =mCanvas.save();
> >                 //mCanvas.drawPicture(picture);
> >                 //mCanvas.restore();
> >         }
> >         catch (Exception e) {
> >                 e.printStackTrace();
> >         }
>
> >         mBitmap =Bitmap.createBitmap(w.getWidth(), w.getHeight
> > (),Config.ARGB_8888);
> >         mCanvas.drawBitmap(mBitmap, 0, 0, null);
>
> >         if(mBitmap!= null) {
> >                 mByteArrayOpStream = new ByteArrayOutputStream();
> >                 mBitmap.compress(Bitmap.CompressFormat.JPEG, 90,
> > mByteArrayOpStream);
> >                 try {
> >                         fos = openFileOutput("yahoo.jpg", 
> > MODE_WORLD_WRITEABLE);
> >                         fos.write(mByteArrayOpStream.toByteArray());
> >                         fos.close();
> >                 } catch (FileNotFoundException e) {
> >                         e.printStackTrace();
> >                 } catch (IOException e) {
> >                         e.printStackTrace();
> >                 }
> >         }
>
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> > Thanks,
> > Ajeet Singh
>
> > On Sep 9, 12:29 pm, gjs <[EMAIL PROTECTED]> wrote:
>
> > > Hi,
>
> > > the code you posted refers to mBitmapScreenshotonce, how did you
> > > instantiate this, what is itcontent? black maybe...
>
> > > your code also refers to screenshot but the only time you access is
> > > when you attempt to compress itintoa byte array, what is it
> > >content? black maybe...
>
> > > Regards
>
> > > PS you won't get much help here, if you can't be bothered to check
> > > your own code...
>
> > > On Sep 9, 4:40 pm, Ajeet Singh <[EMAIL PROTECTED]> wrote:
>
> > > > Hi All,
>
> > > > I am trying to get thewebview'scontenton abitmapso that i can
> > > > save it as a image. But I am unable to so. I am getting the BLACK
> > > > image save instead. Allcontentis missing :(
>
> > > > Anybody can help me regarding this.
>
> > > > Here is my code
>
> > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> > > > //create awebview
> > > >WebVieww = newWebView(this);
>
> > > > //Loads the url
> > > > w.loadUrl("http://www.yahoo.com");
>
> > > > //After loading completely, take its picture
> > > > Picture picture = w.capturePicture();
>
> > > > //Create a new canvas
> > > > Canvas mCanvas = new Canvas();
>
> > > > //Draw the Pictureintothe Canvas
> > > > picture.draw(mCanvas);
>
> > > > //Create aBitmap
> > > >Bitmapsreenshot =Bitmap.createBitmap(picture.getWidth(),
> > > > picture.getHeight(),Config.ARGB_8888);
>
> > > > //copy thecontentfron Canvas toBitmap
> > > > mCanvas.drawBitmap(mBitmapScreenshot, 0, 0, null);
>
> > > > //Save theBitmapto local filesystem
> > > > if(sreenshot != null) {
> > > >         ByteArrayOutputStream mByteArrayOpStream = new
> > > > ByteArrayOutputStream();
> > > >         screenshot.compress(Bitmap.CompressFormat.JPEG, 90,
> > > > mByteArrayOpStream);
> > > >         try {
> > > >                 fos = openFileOutput("yahoo.jpg",
> > > > MODE_WORLD_WRITEABLE);
> > > >                 fos.write(mByteArrayOpStream.toByteArray());
> > > >                 fos.close();
> > > >         } catch (FileNotFoundException e) {
> > > >                 e.printStackTrace();
> > > >         } catch (IOException e) {
> > > >                 e.printStackTrace();
> > > >         }
>
> > > > }
>
> > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> > > > Thanks,
> > > > AJ
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [EMAIL PROTECTED]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---