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.
Related
I am inserting the image using InsertImage, but every time when image is stored on sd card its background become black. How can I remove that black background?
My code is:
> Bitmap Img = BitmapFactory.decodeResource(getResources(),
> R.drawable.ic_launcher); String path =
> Images.Media.insertImage(getContentResolver(), Img, "myImg", "Image");
please use this format before saving images in SD card ----->>> Bitmap.CompressFormat.PNG if you use Bitmap.CompressFormat.JPEG your problem will repeat
public class SDCard {
public void setBitmap(Bitmap bitmap, String filename)
throws FileNotFoundException {
File folder = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/JANU");
if (!folder.exists()) {
folder.mkdir();
}
File imagefile = new File(folder, filename);
FileOutputStream fout = new FileOutputStream(imagefile);
boolean bit = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fout);
}
Im trying to capture user signature and store it under SD Card. App runs without any error.But the image file is not storing to the SD card.it only shows folder named sign.jpg.
Image is missing .please help me to slove this. im new to android..
Here is my current code
public Bitmap save(View v) {
Log.v("log_tag", "Width: " + v.getWidth());
Log.v("log_tag", "Height: " + v.getHeight());
if (mBitmap == null) {
mBitmap = Bitmap.createBitmap(mContent.getWidth(),
mContent.getHeight(), Bitmap.Config.RGB_565);
}
Canvas canvas = new Canvas(mBitmap);
try {
FileOutputStream mFileOutStream = new FileOutputStream(mypath);
v.draw(canvas);
mBitmap.compress(Bitmap.CompressFormat.PNG, 90, mFileOutStream);
mFileOutStream.flush();
mFileOutStream.close();
String url = Images.Media.insertImage(getContentResolver(),
mBitmap, "title", null);
Log.v("log_tag", "url: " + url);
} catch (Exception e) {
Log.v("log_tag", e.toString());
}
return mBitmap;
}
Logcat displays this error message
09-16 17:11:52.517: E/BitmapFactory(1123): Unable to decode stream: java.io.FileNotFoundException: /storage/sdcard0/Captures/sign1945.jpg: open failed: EISDIR (Is a directory)
your problem is in mypath variable
-be sure to use Environment.getExternalStorageDirectory().getPath() to get the SD Card path
-you need to tell the compress method : hey i want to save bitmap to this directory with this name! like this:
//saveDirectoryPath = Environment.getExternalStorageDirectory().getPath() + "/folder/";
File file = new File(saveDirectoryPath);
if (!file.exists())
file.mkdir(); //or file.mkdirs(); depends on your need
File bitmapFile = new File(file, "yourImageFileName.jpg");
FileOutputStream mFileOutStream = new FileOutputStream(bitmapFile);
mBitmap.compress(CompressFormat.PNG, 90, mFileOutStream);
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
This is my code and im trying to capture screenshot of my application.I have background as animation(hearts falling)which looks like a live wallpaper.I want to take screenshot of current page>But its not working.I have used a button to take scrrenshot and imageview to show preview.when button is clicked nothing happens>Iam new to android.Plz help.Thanks in advance!!
View view = findViewById(R.id.Flayout);
view.setDrawingCacheEnabled(true);
Bitmap bitmap = view.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
imgshot = (ImageView) findViewById(R.id.imagescreen);
// set screenshot bitmapdrawable to imageview
imgshot.setBackgroundDrawable(bitmapDrawable);
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState()))
{
// we check if external storage is available, otherwise
// display an error message to the user using Toast Message
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File(sdCard.getAbsolutePath()
+ "/ScreenShots");
directory.mkdirs();
String filename = "screenshot" + i + ".jpg";
File yourFile = new File(directory, filename);
while (yourFile.exists())
{
i++;
filename = "screenshot" + i + ".jpg";
yourFile = new File(directory, filename);
}
if (!yourFile.exists())
{
if (directory.canWrite())
{
try
{
FileOutputStream out = new FileOutputStream(
yourFile, true);
bitmap.compress(Bitmap.CompressFormat.PNG, 90,
out);
out.flush();
out.close();
Toast.makeText(
ResultActivity.this,
"File exported to /sdcard/ScreenShots/screenshot"
+ i + ".jpg",
Toast.LENGTH_SHORT).show();
i++;
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
} else
{
Toast.makeText(ResultActivity.this,
"Sorry SD Card not available in your Device!",
Toast.LENGTH_SHORT).show();
}
break;
}
}
}
Well, check this out:
How to programmatically take a screenshot in Android?
Here looks like what you want to do (as i understand), so test it out.
you have not get the root view. try this code
File file = new File(Environment.getExternalStorageDirectory()+ File.separator + "myimage.jpg");
// create bitmap
Bitmap bitmap;
View v1 = getWindow().getDecorView().getRootView(); //if this didnt work then try sol -3 at the bottom of this answer
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.close();
if this didnt work
View v1 = mCurrentUrlMask.getRootView();
then try this
View v1 = getWindow().getDecorView().getRootView();
or you can do
sol -3 ) View v1 = findViewById(R.id.linear_layout_id);// add the root view id
Don't forget to add permissions or it wont work:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I am trying to use the VuDroid PDF viewer and I need to take the rendered bitmap and store it as a byte[]. Then I need to convert it back into a Bitmap that can be displayed on a view using something like "canvas.drawBitmap(bitmap, 0, 0, paint);".
I have spent many hours trying to access the Bitmap and I might have done it already, but even if I get the byte[] to return something it still wont render as a Bitmap on the canvas.
Could someone please help me here, I must be missing something. Thank you so much.
I believe it is supposed to accessed via...
PDFPage.java .... public Bitmap renderBitmap(int width, int height, RectF pageSliceBounds)
-or-
through Page.java -or- DocumentView.java -or- DecodeService.java
Like I said I have tried all of these and have gotten results I just cannot see where I am going wrong since I cannot render it to see if the Bitmap was called correctly.
Thank you again :)
The doc says the method returns "null if the image could not be decode." You can try:
byte[] image = services.getImageBuffer(1024, 600);
InputStream is = new ByteArrayInputStream(image);
Bitmap bmp = BitmapFactory.decodeStream(is);
I think This will help you:-
Render a byte[] as Bitmap in Android
How does Bitmap.Save(Stream, ImageFormat) format the data?
Copy image with alpha channel to clipboard with custom background color?
if you want to get each pdf page as independent bitmap you should consider that
VuDroid render the pages,
PDFView only display them.
you should use VuDroid functions.
now you can use this example and create your own codes
Example code : for make bitmap from a specific PDF page
view = (ImageView)findViewById(R.id.imageView1);
pdf_conext = new PdfContext();
PdfDocument d = pdf_conext.openDocument(Environment.getExternalStorageDirectory() + "your PDF path");
PdfPage vuPage = d.getPage(1); // choose your page number
RectF rf = new RectF();
rf.bottom = rf.right = (float)1.0;
Bitmap bitmap = vuPage.renderBitmap(60, 60, rf); //define width and height of bitmap
view.setImageBitmap(bitmap);
for writing this bitmap on SDCARD :
try {
File mediaImage = new File(Environment.getExternalStorageDirectory().toString() + "your path for save thumbnail images ");
FileOutputStream out = new FileOutputStream(mediaImage);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
for retrieve saved image:
File file = new File(Environment.getExternalStorageDirectory().toString()+ "your path for save thumbnail images ");
String path = file.getAbsolutePath();
if (path != null){
view = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(path), YOUR_X, YOUR_Y, false);
}
Try this code to check whether bitmap is properly generating or not
PdfContext pdf_conext = new PdfContext();
PdfDocument d = (PdfDocument) pdf_conext.openDocument(pdfPath);
PdfPage vuPage = (PdfPage) d.getPage(0);
RectF rf = new RectF();
Bitmap bitmap = vuPage.renderBitmap(1000,600, rf);
File dir1 = new File (root.getAbsolutePath() + "/IMAGES");
dir1.mkdirs();
String fname = "Image-"+ 2 +".jpg";
File file = new File (dir1, fname);
if (file.exists ())
file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}