Screenshot on android

0

Does anyone know how you can take a screenshot just by pressing the screen?

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

    
asked by AkaMiwel 13.10.2016 в 00:22
source

3 answers

1

Very similar to what Cifus says, but forcing a redraw of 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();
    
answered by 13.10.2016 в 08:50
1

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

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

With this you can call the getDrawingCache method once, which returns a bitmap of the image to finish deactivating the previous thing, it would be like this:

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

A couple of tips, first run in the background, depends on the device can be more or less expensive operation, also to store it, sometimes the bitmap that will return you 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 it would be 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 that you take a look at the options you have for compression and type of format 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 anymore, I recommend you use recycle to free resources, very useful for mobile with few resources.

    
answered by 13.10.2016 в 08:41
0

To capture the screen by code in Android, you can call the following function, 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 next function, passing as a parameter the Bitmap that returns the function that obtains 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();
  }
}

link

    
answered by 08.05.2017 в 14:32