Screenshot on android

Does anyone know how a screenshot can be taken just by pressing the screen?

In the layout I only have a VideoView and I want to take capture of what it captures at that moment

 0
Author: AkaMiwel, 2016-10-12

3 answers

Very similar to what CIFUS comments, but forcing a redraw from the view, with a invalidate . Here view would be the layout you want to capture:

  view.setDrawingCacheEnabled(true);
  view.buildDrawingCache();
  view.invalidate();
  final Bitmap bmp =  view.getDrawingCache();
  view.setDrawingCacheEnabled(false);
  view.destroyDrawingCache();
 1
Author: Fran, 2016-10-13 06:50:37

I understand that your object is to be able to do it by code. About the view you want to work with you should do the following:

view.setDrawingCacheEnabled(true);
view.buildDrawingCache();

With this you can already call once the getDrawingCache method that returns you a bitmap of the image to end up disabling the above, it would be like this:

Bitmap bitmap = view.getDrawingCache();
view.setDrawingCacheEnabled(false);
view.destroyDrawingCache();

A couple of tips, first run in the background, depending on the device can be more or less expensive operation, also to store it, sometimes the bitmap that getDrawingCache can be null, you'll have to deal with it.

Edit: Of course the next step would be to save the bitmap to a file like this:

        File mypath = new File(directory, fileName + ".png");

        FileOutputStream fos;
        try {
            if (!mypath.exists()) mypath.createNewFile();
            fos = new FileOutputStream(mypath);

            bitmapImage.compress(Bitmap.CompressFormat.PNG, 50, fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        bitmapImage.recycle(); 

I recommend you take a look at the compression and formatting options you have in the documentation. By the way the addition of Fran is highly recommended. By the way if you are not going to use the bitmap more I recommend that you use recycle to free resources, very useful for mobile with few resources

 1
Author: Cifus, 2016-10-20 10:17:51

To capture the screen by code on Android, the following function can be called, from an activity.

private Bitmap takeScreenshot() {
  try {
    // crear un bitmap con la captura de pantalla
    View v1 = getWindow().getDecorView().getRootView();
    v1.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);
    return bitmap
  } catch (Throwable e) {
    // Several error may come out with file handling or OOM
    e.printStackTrace();
  }
}

To save the screenshot, it would be enough to call the following function, passing as a parameter The Bitmap that returns the function that gets the capture.

private void saveScreenshot(Bitmap bitmap) {
  Date now = new Date();
  android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

  try {
    // nombre y ruta de la imagen a incluir
    String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";

    File imageFile = new File(mPath);

    FileOutputStream outputStream = new FileOutputStream(imageFile);
    int quality = 100;
    bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
    outputStream.flush();
    outputStream.close();

  } catch (Throwable e) {
    // Captura los distintos errores que puedan surgir
    e.printStackTrace();
  }
}

Https://trellat.es/capturar-la-pantalla-por-codigo-en-android /

 0
Author: user30147, 2017-05-08 12:32:45