0

Hello everyone I will try to be as clear as possible .... I have a method in which I get the Screen of my App, what I would really like is to be able to use my method to get Screen from other apps, or the Android desktop, The way I try to do it is before I capture the screen, I transform my Layout into INVISIBLE, but my capture goes black, if I capture my app if it comes out perfect, but I want to capture other apps, any ideas? ... I show you my method ..

public void addListenerOnButton4() {

    Button btnTakeScreenshot = (Button) findViewById(R.id.share);


    btnTakeScreenshot.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            takeScreenshot();
        }
    });
}
public void takeScreenshot() {

    RelativeLayout ln = (RelativeLayout) findViewById(R.id.Layout);
    ln.setBackgroundColor(Color.TRANSPARENT);
    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
                .getExternalStoragePublicDirectory(Environment
                        .DIRECTORY_DOWNLOADS) +File.separator+now+"ScreenShoot.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();

    } catch (Throwable e) {
        // Several error may come out with file handling or OOM
        e.printStackTrace();
    }
}
  • 2
    On Android 5.0+, you can use the media projection APIs to do this. See [this sample app](https://github.com/commonsguy/cw-omnibus/tree/master/MediaProjection/andshooter). Prior to that, there is no support for screenshots of other apps, except perhaps on rooted devices or through the use of development tools. – CommonsWare Mar 02 '17 at 14:54

1 Answers1

2

- API level 21+

You can use MediaProjectionManager. Details here.

MediaProjection implementation gives you a Bitmap that you can save as JPEG. After you get byteArray just write it to the file as you usually do in Java.

Bitmap bmp = // your Bitmap here
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();

- API before 21

Android gives a way to capture only View but when you make it INVISIBLE there's no color for you View anymore. That is why you're getting the black screen. You can't take a screenshot of other apps just making your View invisible because the capture function connected to the View.

- Any API + root

If you want to do this anyway you need root access. Then you can read framebuffer and get the raw data and convert it into Bitmap. Check details here.

Val
  • 4,225
  • 8
  • 36
  • 55
  • Thanks Val for responding, I understand that I should use MediaProjection in fact I have a project in which I record the screen using MediaPoyection, but I do not understand how to take a screenshot and pass it to jpg from MediaProyection, and check the link that you passed but not I find any code that works – Matias Marco Mar 02 '17 at 15:28
  • `MediaProjection` implementation gives you a `Bitmap` that you can save as JPEG. I updated the answer. The code works well. – Val Mar 02 '17 at 16:01