Skip to content Skip to sidebar Skip to footer

Take A Screen Shot Programmatically

Here you can See take screen shot programmatically and display screen shot in Image View and and check weather SD card is in your device or not , To save screen shot in to the SD C

Solution 1:

Put this on OnCreate

Displaydisplay= getWindowManager().getDefaultDisplay();
@SuppressWarnings("deprecation")intwidth= display.getWidth();
intheight="ParentLayoutOFView".getMeasuredHeight();

createImage(height, width, linearLayout, "FileName");

Add this methods

public File createImage(int height, int width, View view, String fileName) {
    BitmapbitmapCategory= getBitmapFromView(view, height, width);
    return createFile(bitmapCategory, fileName);
}

public File createFile(Bitmap bitmap, String fileName) {

    FileexternalStorage= Environment.getExternalStorageDirectory();
    StringsdcardPath= externalStorage.getAbsolutePath();
    FilereportImageFile=newFile(sdcardPath + "/YourFolderName" + fileName + ".jpg");
    try {
        if (reportImageFile.isFile()) {
            reportImageFile.delete();
        }
        if (reportImageFile.createNewFile()) {
            ByteArrayOutputStreambytes=newByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            FileOutputStreamfo=newFileOutputStream(reportImageFile);
            fo.write(bytes.toByteArray());
            bytes.close();
            fo.close();

            return reportImageFile;
        }
    } catch (Exception e) {
        Toast.makeText(ReportsActivity.this, "Unable to create Image.Try again", Toast.LENGTH_SHORT).show();
    }
    returnnull;
}

public Bitmap getBitmapFromView(View view, int totalHeight, int totalWidth) {

    BitmapreturnedBitmap= Bitmap.createBitmap(totalWidth, totalHeight, Bitmap.Config.ARGB_8888);
    Canvascanvas=newCanvas(returnedBitmap);
    DrawablebgDrawable= view.getBackground();
    if (bgDrawable != null)
        bgDrawable.draw(canvas);
    else
        canvas.drawColor(Color.WHITE);

    view.measure(MeasureSpec.makeMeasureSpec(totalWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(totalHeight, MeasureSpec.EXACTLY));
    view.layout(0, 0, totalWidth, totalHeight);
    view.draw(canvas);
    return returnedBitmap;
}

Post a Comment for "Take A Screen Shot Programmatically"