android webview full page screen capture - android

I am trying to capture webview full page content as a screenshot(bitmap) in android but no luck. Tested many solutions suggested in stackoverflow and other sites. Please help.
Bitmap bitmap = Bitmap.createBitmap(webView.getWidth(), webView.getContentHeight(), Bitmap.Config.ARGB_4444);
webView.draw(new Canvas(bitmap));

You can try this :
WebView w ;
#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( "mnt/sdcard/yahoo.jpg" );
if ( fos != null )
{
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
}
}
catch( Exception e )
{
}
}
});
setContentView(w);
w.loadUrl("http://search.yahoo.com/search?p=android");
}
I found this from hear.. click.
did you try in this way.

Related

How to print whole WebView(Scrollable)?

I want to print the whole WebView on Button Click.
I used Citizen Printer.
I tried Below way.
private Bitmap CitizenWebPrint(WebView webView) {
Bitmap bitmapCitizen = null;
Picture capturePicture = webView.capturePicture();
int width = capturePicture.getWidth();
int height = capturePicture.getHeight();
try {
bitmapCitizen = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
} catch (OutOfMemoryError e) {
Toast.makeText(getApplicationContext(),"2",Toast.LENGTH_LONG).show();
}
capturePicture.draw(new Canvas(bitmapCitizen));
return bitmapCitizen;
}
But Problem is,
This can print only visible part of WebView.
How can I print the whole WebView?
Draw the DecorView object of the current window, and then draw the Bitmap object.
And you can do like this.
Before doing it ,you can do this.
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webView.requestFocusFromTouch();
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl("your url");
And check version.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkSdkVersion();
setContentView(R.layout.activity_webview_capture);
}
// check version
private void checkSdkVersion() {
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
WebView.enableSlowWholeDocumentDraw();
}
}
Add permission in your manifest.
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Way 1
private void getScreenshot() {
float scale = webView.getScale();
int webViewHeight = (int) (webView.getContentHeight() * scale + 0.5);
Bitmap bitmap = Bitmap.createBitmap(webView.getWidth(), webViewHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
webView.draw(canvas);
// save to the File
try {
String fileName = Environment.getExternalStorageDirectory().getPath() + "/webview_capture1.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
// Save
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
fos.close();
Toast.makeText(WebviewFromDraw.this, "Screenshot OK", Toast.LENGTH_LONG).show();
bitmap.recycle();
} catch (Exception e) {
e.getMessage();
}
}
Way 2 use webView.getDrawingCache();
private void getScreenshot() {
bitmap = webView.getDrawingCache();
try {
String fileName = Environment.getExternalStorageDirectory().getPath()+"/webview_capture2.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
fos.close();
Toast.makeText(WebviewFromDrawCache.this, "Screenshot OK", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("TAG", e.getMessage());
}
}
#Override
protected void onDestroy() {
super.onDestroy();
//recycle
if(bitmap!=null) {
bitmap.recycle();
}
}
Way 3 the same to yours,it works well in my device.
private void getScreenshot() {
Picture picture = webView.capturePicture();
int width = picture.getWidth();
int height = picture.getHeight();
if (width > 0 && height > 0) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
picture.draw(canvas);
try {
String fileName = Environment.getExternalStorageDirectory().getPath()+"/webview_capture3.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
fos.close();
Toast.makeText(WebviewFromCapture.this, "Screenshot ok", Toast.LENGTH_LONG).show();
bitmap.recycle();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
}

How to capture a complete view of an Activity Screen in Android

I am using a scroll View in my activity.I want to share the whole activity screen but not able to do that.Am able to share the visible part of the screen but not the full screen of the activity.Is it possible to take the screenshot of the full activity.I have tried like this but its sharing only the visible part of the activity.Plz help me out of this.Thank You
ShareImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//View v1 = getWindow().getDecorView();
View v1 = findViewById(android.R.id.content);
v1.setDrawingCacheEnabled(true);
myBitmap = v1.getDrawingCache();
saveBitmap(myBitmap);
}
});
Saving the Screenshot taken ::
public void saveBitmap(Bitmap bitmap) {
String filePath = Environment.getExternalStorageDirectory()
+ File.separator + "Pictures/screencapture.png";
File imagePath = new File(filePath);
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
share(filePath);
} catch (FileNotFoundException e) {
Log.e("exception1", e.getMessage(), e);
} catch (IOException e) {
Log.e("exception2", e.getMessage(), e);
}
}
try this:
public static Bitmap loadBitmapFromView(View v, int width, int height) {
Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}

Android Webview, trying to save as image file only seems to store a blank image

I must be doing something wrong here with saving the image or something, as when I save it as a png, its a blank white image. When I save it as a JPEG, it's a blank black image.
The webview loads in my application after about 1 second.
.....
webView = (WebView)findViewById(R.id.webview_1);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
//Bitmap b = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Bitmap b = getBitmapFromWebView(webView);
Canvas c = new Canvas(b);
view.draw(c);
try {
FileOutputStream fos = openFileOutput("test.png", Context.MODE_APPEND);
//fos = new FileOutputStream( "/sdcard/" + "page.jpg" );
if ( fos != null ) {
b.compress(Bitmap.CompressFormat.PNG, 90, fos );
fos.close();
}
}
catch( Exception e ) {
System.out.println("-----error--"+e);
}
}
});
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
webView.loadUrl("file:///android_asset/World_display.html");
....
public Bitmap getBitmapFromWebView(WebView wv) {
webView.setDrawingCacheEnabled(true);
webView.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(wv.getDrawingCache());
webView.setDrawingCacheEnabled(false);
return bitmap;
}
Please try this:
public static Bitmap getBitmapfromView(View v) {
Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
return bitmap;
}
As it turns out, the webview isn't rendered until sometime after onPageFinished. All of this code being in my onCreate was saving a blank image because the webview hadn't been rendered yet.
Ended up doing this as a separate method outside onCreate:
public Bitmap getBitmapFromWebView(View v) {
webView.setDrawingCacheEnabled(true);
webView.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
try {
FileOutputStream fos = openFileOutput("test.png", Context.MODE_PRIVATE);
//fos = new FileOutputStream( "/sdcard/" + "page.jpg" );
if (fos != null) {
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
}
} catch (Exception e) {
System.out.println("-----error--" + e);
}
return bitmap;
}
And calling this method from within a "submit" button onclick in the onCreate.

Snapshot the webview return blank image

I want to snapshot the entire of the content of the WebView. However, the returned image is always blank. Can you take a look and tell me where I'm wrong.
Please don't suggest me use capturePicture function because the function is deprecated at this time.
Some codes
public class WebViewActivity extends Activity {
private static WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.loadUrl("http://developer.android.com/training/basics/firstapp/creating-project.html");
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
webView.measure(MeasureSpec.makeMeasureSpec(
MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
webView.layout(0, 0, webView.getMeasuredWidth(),
webView.getMeasuredHeight());
webView.setDrawingCacheEnabled(true);
webView.buildDrawingCache();
Bitmap bm = Bitmap.createBitmap(webView.getMeasuredWidth(),
webView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas bigcanvas = new Canvas(bm);
Paint paint = new Paint();
int iHeight = bm.getHeight();
bigcanvas.drawBitmap(bm, 0, iHeight, paint);
if (bm != null) {
try {
String path = Environment.getExternalStorageDirectory()
.toString();
OutputStream fOut = null;
File file = new File(path, "/aaaa.png");
fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
bm.recycle();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
The layout.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Thank you very much!!!
I found out the solution. Take a look of this.
I used the onDraw to replace the capturePicture functon which is deprecated in API 19. If you have any better solutions, please tell me and I appreciate that. Thanks!
Which can replace capturePicture function

how take screen shot of currently playing video?

I'm trying to take a screenshot of the currently playing video. I'm trying with code that successfully takes a screenshot of web view but get not success in taking photo of currently playing video.
The code as follow for web view.
WebView 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");
To expand on 66CLSjY's answer, FFmpegMediaMetadataRetriever has the same interface as MediaMetadataRetriever but it uses FFmpeg as the backend. If the default configuration won't work with your video format you can enable/disable codecs by recompiling. Here is some sample code:
FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
mmr.setDataSource(mUri);
mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_VIDEO_CODEC);
Bitmap b = getFrameAtTime(3000);
mmr.release();
try this it will gives bitmap for your app screen
View v = view.getRootView();
v.setDrawingCacheEnabled(true);
Bitmap b = v.getDrawingCache();
This works for me:
First a method to convert your view into a bitmap
public static Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(),view.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}
Then save into the SD, for example:
static private boolean saveImage(Bitmap bm, String absolutePath)
{
FileOutputStream fos = null;
try
{
String absolutePath = "your path"
File file = new File(absolutePath);
fos = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, fos); //PNG ignora la calidad
} catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (fos != null)
fos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
return true;
}
Good Luck!

Categories

Resources