Android webview screenshot white border - android

i followed this link to screenshot my webview completely. It works fine.
But I have an issue. The screenhot that I get from the webview contains white spaces on the top and bottom of the picture? How do I get rid of this? Kindly let me know.
This is the code that I've used for capturing from webview. IF YOU LOOK PROPERLY THERE IS WHITESPACE BOTH AT THE BOTTOM AND TOP PARTS OF THE IMAGE.
public static String saveCapturedImageToSdCard() {
Picture picture = WebViewActivity.sWebViewEx.capturePicture();
Bitmap b = Bitmap.createBitmap( picture.getWidth(),
picture.getHeight(), Bitmap.Config.ARGB_8888);
File file = null;
Canvas c = new Canvas( b );
picture.draw( c );
// FileOutputStream fos = null;
try {
file = new File(Environment.getExternalStorageDirectory()+"/"+"temp.png");
if(file != null && !file.exists())
file.createNewFile();
// write the bytes in file
FileOutputStream fos = new FileOutputStream(file);
fos = new FileOutputStream(file);
if ( fos != null )
{
b.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
}
}
catch( Exception e )
{
}
return file.getPath().toString();
}
This is the image that I get from the webview:
My XML is:
<WebView
android:id="#+id/webview"
android:layout_below="#id/linearlayout_topbuttons"
android:layout_above="#id/gallery_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:overScrollMode="never"
android:fitsSystemWindows="true"
android:background="#android:color/transparent"
android:layout_centerInParent="true"
/>

Related

webview saves same screenshot again android

The below code captures the webview image and save it in Pictures folder. The code is as follows
public static void getBitmapOfWebView(final WebView webView){
Date now = new Date();
String mPath = Environment.getExternalStorageDirectory() + File.separator + Environment.DIRECTORY_PICTURES + File.separator + "Screenshot_" + now + ".jpg";
webView.setDrawingCacheEnabled(true);
webView.buildDrawingCache(true);
Picture picture = webView.capturePicture();
Bitmap b = Bitmap.createBitmap( webView.getWidth(), webView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
picture.draw(c);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mPath);
if ( fos != null ) {
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
}
}
catch(Exception e) {}
b.recycle();
}
The above code works and saves the screenshot in the pictures folder. But the problem is when i scroll down the WebView and try to execute the same code again, it saves the old screenshot of the webview in the pictures folder.
How do i code in such a way that when i scroll the Webview, it should save the current screenshot of the webview.
Any help Appreciated.
Thank you.

Taking screenshot of a view gives black image?

I tried to take a screenshot of a imageview programatically! When the image is saved to folder it is producing black image! when i tried to take screenshot of my entire scroll view, I am able to take screenshot. Please help:
my Code is:
private void saveop(){
RelativeLayout parent = (RelativeLayout) findViewById(R.id.opimagelayout);// which includes your view or buttons?
Bitmap bitmap = Bitmap.createBitmap(384,
100, Bitmap.Config.ARGB_8888);
Canvas bitmapHolder = new Canvas(bitmap);
parent.draw(bitmapHolder);
try {
final File f = savebitmaps(bitmap,"operator","");
} catch (IOException e) {
e.printStackTrace();
}
}
public static File savebitmaps(Bitmap bmp,String transno, String currentDateandTime) throws IOException {
Bitmap out = BITMAP_RESIZER(bmp, 384,112);
File folder = new File(Environment.getExternalStorageDirectory() + "/MobeeLoad");
File file = new File(folder, transno+".jpg");
FileOutputStream fOut;
try {
fOut = new FileOutputStream(file);
out.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
bmp.recycle();
out.recycle();
} catch (Exception e) {
}
bmp.recycle();
return file;
}
Layout xml:
<RelativeLayout
android:id="#+id/opimagelayout"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_below="#+id/date"
android:layout_height="90dp">
<ImageView
android:id="#+id/opimage"
android:layout_width="90dp"
android:src="#drawable/check"
android:layout_centerHorizontal="true"
android:layout_height="90dp" />
</RelativeLayout>

How to convert HTML files (stored in assets) to image in android?

I'm trying to convert html files stored as assets to image so that I can share them. I tried the following code:
Picture picture = webView.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( Environment.getExternalStorageDirectory().toString() +"/temp.jpg");
if ( fos != null ) {
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
Toast.makeText(SyllabusPage_NW.this, "created", Toast.LENGTH_SHORT).show();
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse(Environment.getExternalStorageDirectory().toString() + "/temp.jpg");
sharingIntent.setType("image/jpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
fos.close();
}
}
catch( Exception e ) {
Toast.makeText(SyllabusPage_NW.this, "Error", Toast.LENGTH_SHORT).show();
}
The result of this is attached and the expected result is also attached. The webview captures only the first part of the html page.
I wanna know how I can capture the entire html page instead of just the first bit?
for API Lollipop and higher should enable slow draw:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
WebView.enableSlowWholeDocumentDraw();
}
then check the height of picture is correct by debugging.
if not measre webview first then layout.
webView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
webView.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
webView.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(webView.getDrawingCache());
webView.setDrawingCacheEnabled(false);

How to take screenshot of webview in Android

I have drawn some lines on html5 canvas within webview and tried to take screenshot of the webview using below code...
WebView webView = (WebView) findViewById(R.id.webview);
webView.setDrawingCacheEnabled(true);
Bitmap screenshot = Bitmap.createBitmap(webView.getDrawingCache());
webView.setDrawingCacheEnabled(false);
File myFile = new File(Environment.getExternalStorageDirectory().getPath()+ "/myfolder");
if(!myFile.exists()) {
myFile.mkdir();
}
imagePath = myFile.getAbsolutePath() + "/myimage001.png";
FileOutputStream fos = null;
try {
fos = new FileOutputStream(imagePath);
if ( fos != null ) {
screenshot.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
}
} catch( Exception e ) {
e.printStackTrace();
}
But when I open that image then it looks empty. Please help.
Do as follows
private void TakeScreenshot()
{
Picture picture = webview.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 )
{
}
}
N.B: Take the screenshot after the WebView finishes loading otherwise you will get a blank screen.
I too found taking screenshots of webview displaying a HTML5 canvas directly from android app really unreliable. But there is a workaround -
You can use window.JSInterface.(params) to call an android java function from javascript. You can use canvas.toDataURL() to get a base64 encoded string of the canvas image and use it as the parameter to pass to the function. In the android function, capture the string, decode it and you have the image.
The only caveat is that the canvas area alone will come in as image and not the full device screen. Also, if the canvas didn't have a background color set, the captured png image will have a transparent background. But these can be easily solved by image manipulation from the app code.
E.g. In this game app, the game runs in a HTML5 canvas displayed in a webview. When the game finishes, a "share score" option uses the above technique to capture the canvas image to share.
https://play.google.com/store/apps/details?id=com.skipser.flappytrex
To capture screenshot:
File srcFile= driver.getScreenshotAs(OutputType.FILE);
String filename = UUID.randomUUID().toString();
File targetFile = new File("D:\\Appium\\" + filename + ".png");
FileUtils.copyFile(srcFile, targetFile);
System.out.println(targetFile);enter code here

Android -- taking a screen shot

I need to take a screen shot and save the screen shot. I need to do this without using any connection to PC or without unrooting the phone. I need to do this whenever a event is triggered . For example when an ad is shown in a game ... or when the game ends and shows the scores in snake etc. Can you please let me know How Can i do this. I saw some tutorilas and they gave the code but that doesnt seem to work
private void getScreen()
{
View content = findViewById(R.id.layoutRoot);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/sdcard/test.png");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
You have to enable the cache first, before calling getDrawingCache().
View ve = findViewById(R.id.loyout);
ve.setDrawingCacheEnabled(true);
Bitmap b = ve.getDrawingCache();
String extr = Environment.getExternalStorageDirectory().toString()
+ "/SaveCapture";
myPath = new File(extr);
if (!myPath.exists()) {
boolean result = myPath.mkdir();
Toast.makeText(this, result + "", Toast.LENGTH_SHORT).show();
}
myPath = new File(extr, getString(R.string.app_name) + ".jpg");
Toast.makeText(this, myPath.toString(), Toast.LENGTH_SHORT).show();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(getContentResolver(), b,
"Screen", "screen");
}
catch (FileNotFoundException e) {
Log.e("Error", e + "");
}
catch (Exception e) {
Log.e("Error", e + "");
}
Can you give more information as to what does not work when you run that code ? Does it not capture what you want ? Does it crash ?
Make sure you change the R.id.layoutroot correctly with your root layout... Beside that it seems like it would work...
<com.example.android.snake.SnakeView
android:id="#+id/snake"
android:layout_width="match_parent"
android:layout_height="match_parent"
tileSize="24"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/text"
android:text="#string/snake_layout_text_text"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:textColor="#ff8888ff"
android:textSize="24sp"/>
</RelativeLayout>
Edit...
So for example, if you use that layout you just put there, you should change the R.id.layout into R.id.snake, that's because of this line : android:id="#+id/snake".
I don't think there is an easy way to find /get the id of the "root" layout of a view (if you wanted to take screenshot of anything the phone is showing.
I just checked the launcher and another application, seems like most app are placed into a FrameLayout with id/content, so you can try to use android.R.id.content, but there is no guaranty this will work every time...

Categories

Resources