Taking android screen shot of whole screen - android

My application has a list view in relative layout. Now I need to take a screen shot of the entire layout including all the content available in the list view and also other contents like button and text view showing in the layout. But my code takes only the visible part of the screen. Here the view is parent layout.
public void screen(){
View v1 = findViewById(R.id.screenRoot);
rel.layout(0, 0, rel.getMeasuredWidth(),rel.getMeasuredHeight());
View v = v1.getRootView();
v.setDrawingCacheEnabled(true);
/* v.measure(MeasureSpec.makeMeasureSpec(w, w),
MeasureSpec.makeMeasureSpec(h, h));*/
// v.layout(0, 0, rel.getWidth(), rel.getHeight());
v.buildDrawingCache(true);
System.out.println("rel "+rel.getHeight()+" "+v.getHeight());
Bitmap b = v.getDrawingCache();
v.setDrawingCacheEnabled(false);
String extr = Environment.getExternalStorageDirectory().toString();
File myPath = new File(extr, "tiket"+".jpg");
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");
System.out.println("my path "+myPath+" "+fos);
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Image Captured Successfully", 0).show();
}

Call getBitmapFromView from your Click event or any thing.
R.id.root is my main RelativeLayout So you can pass you main layout.
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
RelativeLayout root = (RelativeLayout)inflater.inflate(R.layout.youlayourfile,null);
root.setDrawingCacheEnabled(true);
Bitmap bmp = getBitmapFromView(this.getWindow().getDecorView().findViewById(R.id.root).getRootView());
URI uri = storPrintFile(bmp);
This function returns the Bitmap of your layout. Now you just need to store the bitmap into your device and anywhere else.
public Bitmap getBitmapFromView(View v) {
v.setLayoutParams(new LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(),
v.getMeasuredHeight(), Bitmap.Config.RGB_565);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}
Storing bitmap into device.
public URI storPrintFile(Bitmap bitmapToStore){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
String path = cw.getDir("CapturedImages",Context.MODE_PRIVATE).getPath();
File file = new File(path);
boolean isFileCreated = false;
if (!file.exists()) {
isFileCreated = file.mkdir();
System.out.println("Directory created in Internal Storage is :" + path);
}
String current = "Screen.jpg";//uniqueId.replace(" ", "-").replace(":", "-") + ".jpeg";
System.out.println("Internal Storage path is : " + current);
File mypath = new File(file, current);
try {
FileOutputStream out = new FileOutputStream(mypath);
bitmapToStore.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return mypath.toURI();
}

Related

Capture a part of application and save it

In my app i want to capturea a part of my android application UI and save it programmatically .
For example i want do this actions :
In Activity/Fragment user clicks one Button .
capture from a part of Layout for example a LinearLayout that have id="captureMe" .
Save captured image somewhere .
how i can implement it ?
You can simply use this function just pass your view object
public Bitmap viewToBitmap(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
Then Save this file
public void saveImage(Bitmap inImage) {
String root = Environment.getExternalStorageDirectory().toString();
File mydir = new File(root + "/demo/");
mydir.mkdirs();
String fname = "Image.jpeg";
File file = new File (mydir, fname);
String path2=file.getPath();
Uri uri=Uri.fromFile(file);
try {
FileOutputStream out = new FileOutputStream(file);
inImage.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
}catch (Exception e)
{
e.printStackTrace();
}
}
Try this, hope it works
LinearLayout captureMe = (LinearLayout)findViewById(R.id.captureMe);
captureMe.setDrawingCacheEnabled(true);
captureMe.buildDrawingCache();
bitmap = captureMe.getDrawingCache();
First Use this function to get bitmap of view that you want to capture:
public static Bitmap getViewBitmap(View v, int width, int height) {
Bitmap viewBitmap = Bitmap.createBitmap(width , height,
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(viewBitmap);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return viewBitmap;
}
Then use this code to save this bitmap to storage:
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOutputStream = null;
File file = new File(path + "/Captures/", "screen.jpg");
try {
fOutputStream = new FileOutputStream(file);
capturedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOutputStream);
fOutputStream.flush();
fOutputStream.close();
MediaStore.Images.Media.insertImage(getContentResolver(),
file.getAbsolutePath(), file.getName(), file.getName());
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
return;
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
return;
}

Scrollview to PDF View giving blur in Android

I have long scrollview almost 50+ fields.
I'm converting that scrollview to pdf view. pdf also creating all done.
But pdf file showing data is blur (too blur).
With some merging, I provided some reference image (I mean output image).
output image
My code:
private void takeScreenShot() {
try {
//here getScroll is my scrollview id
View u = ((Activity) mContext).findViewById(R.id.getScroll);
ScrollView z = (ScrollView) ((Activity) mContext).findViewById(R.id.getScroll);
int totalHeight = z.getChildAt(0).getHeight();
int totalWidth = z.getChildAt(0).getWidth();
Bitmap bitmap = getBitmapFromView(u,totalHeight,totalWidth);
Image image;
//Save bitmap
String path = Environment.getExternalStorageDirectory()+"/Folder/";
String fileName = "report1.pdf";
File dir = new File(path);
if (!dir.exists())
dir.mkdirs();
Log.v("PDFCreator", "PDF Path: " + path);
File myPath = new File(path, fileName);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 10, stream);
image = Image.getInstance(stream.toByteArray());
image.setAbsolutePosition(0, 0);
Document document = new Document(image);
PdfWriter.getInstance(document, new FileOutputStream(myPath));
document.open();
document.add(image);
document.close();
} catch (Exception i1) {
i1.printStackTrace();
}
}
Any help?
Try this code:
View u = ((Activity) mContext).findViewById(R.id.getScroll);
u.setDrawingCacheEnabled(true);
u.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(u.getDrawingCache());
bitmap.setHasAlpha(true); //important for PNG
---------------DO SAVE WORK-----------------
v1.setDrawingCacheEnabled(false);
And at
bitmap.compress(Bitmap.CompressFormat.JPEG, 10, stream);
change 10 to 90 or 100. it is Image Quality.
Or just change in your code to higher quality.
Try another solution:
ScrollView v= (ScrollView)findViewById(R.id.getScroll);
int height = v.getChildAt(0).getHeight()
int width = v.getWidth()
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);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
Take a screen shot using this code and
ScrollView iv = (ScrollView) findViewById(R.id.scrollView);
Bitmap bitmap = Bitmap.createBitmap(
iv.getChildAt(0).getWidth(),
iv.getChildAt(0).getHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
c.drawColor(Color.WHITE);
iv.getChildAt(0).draw(c);
// Do whatever you want with your bitmap
saveBitmap(bitmap);
and save the bitmap as a image
public void saveBitmap(Bitmap bitmap) {
File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}}
I might be late but anyway. Use png format instead, increase the quality to 100 and replace
View u = ((Activity) mContext).findViewById(R.id.getScroll);
with
View u = ((Activity) mContext).findViewById(R.id.yourfirstscrollviewchild);
Thatll do the trick.

android- Taking screenshot of a particular view programmatically does not work

I want to take screenshot of a layout from another activity. When I take view into Bitmap it shows NullPointerExcenption. Here is my code
View v=LayoutInflater.from(this).inflate(R.layout.score_share, null);
layoutScore.setDrawingCacheEnabled(true);
Bitmap bm= Bitmap.createBitmap(v.getDrawingCache());
layoutScore.setDrawingCacheEnabled(false);
File file= new File(Environment.getExternalStorageDirectory()+"/scs.jpg");
try {
FileOutputStream outputStream= new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG,100, outputStream);
outputStream.flush();
outputStream.close();
openScreenshot(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Below is the method to capture screenshot.
public Bitmap getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
return bitmap;
}
Store the Bitmap into the SDCard:
public void store(Bitmap bm, String fileName){
final static String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots";
File dir = new File(dirPath);
if(!dir.exists())
dir.mkdirs();
File file = new File(dirPath, fileName);
try {
FileOutputStream fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Now Inflate your layout and capture screen shot
View view=LayoutInflater.from(this).inflate(R.layout.score_share, null);
Bitmap bmp = getScreenShot(view);
bmp is the required screenshot then save it to SDcard like
store(bmp, "Screenshot.jpg");
Don't forget to add Write External Storage permission in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Thanks to this answer Capture Screenshot and store to sdcard
If the problem is getDrawingCache returning null, then just add this lines of code before v.buildDrawingCache(true);
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
This will prevent that the view has a size of (0,0), and because of that it turns null safe.

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;
}

take screenshot of entire screen programmatically

I have this code for take screenshot of current view, a fragment that lives into an activity, where the activity has only a background.
private File captureScreen() {
Bitmap screenshot = null;
try {
if (view != null) {
screenshot = Bitmap.createBitmap(view.getMeasuredWidth(),
view.getMeasuredHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(screenshot);
view.draw(canvas);
// save pics
File cache_dir = Environment.getExternalStorageDirectory();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
screenshot.compress(Bitmap.CompressFormat.PNG, 90, bytes);
File f = new File(cache_dir + File.separator + "screen.png");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
return f;
}
} catch (Exception e) {
// TODO
}
return null;
}
but bitmap saved is not exactly what i'm expecting.
Screenshot take only fragment elements, but not activity background. How can i include it into screenshot?
From :How to programmatically take a screenshot in Android?
// 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();
}
try this. it work for me. and for you too
Call this method, passing in the outer most ViewGroup that you want a screen shot of:
public Bitmap screenShot(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
I've used it for a while in a few different apps and haven't had any issues. Hope it vll helps

Categories

Resources