4

I have html source and with a WebView i load this source with:

WebView webView = (WebView) new WebView(getActivity()); 
        webView = (WebView) rootView.findViewById(R.id.webView1);

        webView.getSettings().setJavaScriptEnabled(true);
        String sourceHtml = (String) this.getActivity().getIntent().getExtras().get(ROW_ID1);


        webView.loadData(sourceHtml, "text/html", "UTF-8");

Now i want to save this WebView in BitMap or other in the SdCard, i follow some guides but never works, how i can do it?

FelasDroid
  • 623
  • 3
  • 10
  • 26

1 Answers1

2

Try to use the capturePicture function:

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( "mnt/sdcard/screenshot.jpg" );
        if ( fos != null )
        {
            b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.close();
        }
    }
catch(Exception e) {}
TylerH
  • 20,799
  • 66
  • 75
  • 101
StarsSky
  • 6,721
  • 6
  • 38
  • 63
  • i have this error: 01-04 14:31:38.930: E/AndroidRuntime(32609): java.lang.NullPointerException 01-04 14:31:38.930: E/AndroidRuntime(32609): at felastech.websourcerequest.WebChange.onCreateView(WebChange.java:65) 01-04 14:31:38.930: E/AndroidRuntime(32609): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478) 01-04 14:31:38.930: E/AndroidRuntime(32609): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927) – FelasDroid Jan 04 '14 at 13:33
  • in this stantment: Picture picture = webView.capturePicture(); return null..why? – FelasDroid Jan 04 '14 at 13:35
  • try to use it inside the onPageFinished http://stackoverflow.com/a/15360880/2015318 – StarsSky Jan 04 '14 at 13:52
  • now i have this: 01-04 15:42:24.760: E/AndroidRuntime(7139): FATAL EXCEPTION: main 01-04 15:42:24.760: E/AndroidRuntime(7139): java.lang.IllegalArgumentException: width and height must be > 0 01-04 15:42:24.760: E/AndroidRuntime(7139): at android.graphics.Bitmap.createBitmap(Bitmap.java:638) 01-04 15:42:24.760: E/AndroidRuntime(7139): at android.graphics.Bitmap.createBitmap(Bitmap.java:620) 01-04 15:42:24.760: E/AndroidRuntime(7139): at felastech.WebChange$1.onPageFinished(WebChange.java:69) – FelasDroid Jan 04 '14 at 14:43