How to take screenshot of entire screen in Android programmatically? - android

I need take screenshot of entire screen in Android, I've searched a lot but they all talked about taking screenshot of specified view, how can I take screenshot of entire screen?
I mean, by program.(Not by DDMS)

There is a library available for taking snapshot through the device its called ASL(Android Screenshot library).
Have a look here with complete source code

In eclipse go to DDMS perspective and select your device. Then click on screen capture(camera picture) button.
Go through this link it may be helpful for you...

Try below code:
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;
// create bitmap screen capture
Bitmap bitmap;
View v1 = mCurrentUrlMask.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
OutputStream fout = null;
imageFile = new File(mPath);
try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Refer to this answer.

You need to root your device otherwise it won't work. Also u have to make ur application get SuperUser access. Just implement this code and you will be good to go:
public void screenShot() throws InterruptedException
{
Process sh;
try
{
sh = Runtime.getRuntime().exec("su", null, null);
OutputStream os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + "/sdcard/Image.png").getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

This code return screenshot of visible and unvisible part of layout.
private Bitmap getScreenBitmap() {
View v = getWindow().getDecorView().findViewById(android.R.id.content);
v.setDrawingCacheEnabled(true);
int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
v.measure(measureSpec, measureSpec);
width = v.getMeasuredWidth();
height = v.getMeasuredHeight();
v.layout(0, 0, width, height);
v.buildDrawingCache(true);
//Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
v.draw(canvas);
v.setDrawingCacheEnabled(false);
return b;
}

In Eclipse go to Window -> Show View -> Other -> Devices
Select your device and then simply click on "camera picture":

Related

Screenshoting certain part of screen programmatically

In my app I have a rectangle ImageView that contains an white background with a hole cut out from the middle. Behind it I have set a camera preview so only the hole shows the camera. How would I just take a picture of the IMAGEVIEW.X AND Y AND HEIGHT AND WIDTH so I can still see the face. Basically crop the screenshot to the size of the imageview
Hope this Helps.
private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
}

How to get the bitmap of current layout(For sharing the screen as image) instead of taking screenshot?

How to get the bitmap of current layout(For sharing the screen as image) instead of taking screenshot? I'm getting the view of layout using following line,
I can able to get bitmap of this layout but the image is looking as empty(I got blank image). This is my code.
View cardShareView = getLayoutInflater().inflate(R.layout.activity_cad_profile, null);
boolean imageResult=saveImageInLocal(getBitmapFromView(cardShareView), "cad_share.png") == true
public static Bitmap getBitmapFromView(View view) {
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.draw(canvas);
return bitmap;
}
private boolean saveImageInLocal(Bitmap imageData, String filename) {
String iconsStoragePath = Environment.getExternalStorageDirectory() + "/KKMC/";
File sdIconStorageDir = new File(iconsStoragePath);
//create storage directories, if they don't exist
sdIconStorageDir.mkdirs();
try {
filePath = sdIconStorageDir.toString() + filename;
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
//choose another format if PNG doesn't suit you
imageData.compress(Bitmap.CompressFormat.PNG, 70, bos);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
Log.w("TAG", "Error saving image file: " + e.getMessage());
return false;
} catch (IOException e) {
Log.w("TAG", "Error saving image file: " + e.getMessage());
return false;
}
return true;
}
Advance Thanks for your response.
Expecting this image
Bit I got this image
Finally, I created duplicate layout(activity_cad_profile.xml) for take the full view as image which is loaded at the time of main layout is loading. I don't know this is correct way or not. But it's working fine for me.

Save layout as an image

I have a LinearLayout and I am want to save the contents of that view as an image. I have it half working.
File imageFile;
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/a.png";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
Bitmap bitmap;
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
OutputStream fout = null;
imageFile = new File(mPath);
try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 10, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
With the above code it will save a copy of the view, but only what's seen on the device screen. I have more information on the view that I want saved. The code above from here and this will only save the information seen on the screen as an image. I want all the information saved, even the information that is not on the screen (where you need to scroll to see).
How can I achieve that?
Another option is to use:
Bitmap b = Bitmap.createBitmap(width, height....)
v1.setLayoutParams // Full width and height of content
Canvas c = new Canvas(b);
v1.draw(c); // You now have full bitmap
saveBitmap(b);
Run a measure/layout pass on it and draw it to a canvas. Suppose your parent was called "view" and was a vertical LinearLayout:
view.measure(someWidth, MeasureSpec.UNSPECIFIED);
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.draw(c);

Crop screenshot in android programmatically

for taking screen shot i have am using below code
public void takeScreenShot(){
File wallpaperDirectory = new File("/sdcard/Hello Kitty/");
if(!wallpaperDirectory.isDirectory()) {
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
// create a File object for the output file
}
File outputFile = new File(wallpaperDirectory, "Hello_Kitty.png");
// now attach the OutputStream to the file object, instead of a String representation
// create bitmap screen capture
Bitmap bitmap;
View v1 = mDragLayer.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache(),0,0,v1.getWidth(),v1.getHeight());
v1.setDrawingCacheEnabled(false);
OutputStream fout = null;
try {
fout = new FileOutputStream(outputFile);
// Bitmap bitMap = Bitmap.createBitmap(src)
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
and now i want a cropped bitmap in that i want to crop some portion from left and some portion from bottom so i have used code like this
Bitmap.createBitmap(v1.getDrawingCache(),v1.getWidth()/10,v1.getHeight()/10,v1.getWidth(),v1.getHeight());
but i got an error
08-29 23:41:49.819: E/AndroidRuntime(3486): java.lang.IllegalArgumentException: x + width must be <= bitmap.width()
08-29 23:41:49.819: E/AndroidRuntime(3486): at android.graphics.Bitmap.createBitmap(Bitmap.java:410)
08-29 23:41:49.819: E/AndroidRuntime(3486): at android.graphics.Bitmap.createBitmap(Bitmap.java:383)
can anybody tell me how to crop portion of an bitmap from left and bottom thanks...
It appears you misunderstood the usage of that specific Bitmap.create(...) function. Rather than supplying the source's width and height as the last two parameters, you should specificy the width and height the cropped result should end up with.
The error explains that since you specified an offset from the left and top, but passed in the source's dimensions, the cropped result would exceed the bounds of the original image.
If all you want to do is crop a tenth off the left and top, simply subtract the offsets from the original width/height:
Bitmap source = v1.getDrawingCache();
int x = v1.getWidth()/10;
int y = v1.getHeight()/10
int width = source.getWidth() - x;
int height = source.getHeight() - y;
Bitmap.createBitmap(source, x, y, width, height);
Instead of using
Bitmap.createBitmap(v1.getDrawingCache(),v1.getWidth()/10,v1.getHeight()/10,v1.getWidth(),v1.getHeight());
You can use bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Refer here for some more methods Bitmap

android: convert canvas to bitmap then save to SD card

i'm developing an android app where I can draw on a canvas. I want to convert my canvas to bitmap and then save it in jpeg format on my sd card..
how can i properly do this?
Something like that should work :
http://developer.android.com/reference/android/view/View.html#getDrawingCache(boolean)
public void toJPEGFile(){
File folder = new File(Environment.getExternalStorageDirectory()+"/folder/");
if(!folder.exists()) folder.mkdirs();
try {
this.setDrawingCacheEnabled(true);
FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory()+"/folder/file"));
Bitmap bitmap = this.getDrawingCache();
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Did you look up the documentation?
Canvas API
You can use the:
public Canvas (Bitmap bitmap)
Since: API Level 1
Construct a canvas with the specified bitmap to draw into. The bitmap must be mutable.
The initial target density of the canvas is the same as the given bitmap's density.
Parameters
bitmap Specifies a mutable bitmap for the canvas to draw into.

Categories

Resources