2

I have a screen capture method that does half the job, it takes the screen shot but the images with PNG extention have transparent background and with JPEG it displays Black bakground. How can I capture the background (desktop wallpaper) ?

public void takeScreenshot() throws IOException {

    date = new DateIAinnot();
    view = ((Activity) context).getWindow().getDecorView().getRootView();
    view.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(view.getDrawingCache());
    view.setDrawingCacheEnabled(false);

    root = Environment.getExternalStorageDirectory().toString();
    myDir = new File(root + "/Pictures");
    myDir.mkdirs();

    fname = date.getDate(2) + ".png";
    fname = fname.replace(':', '_');
    fname = fname.replace(' ', '-');
    file = new File(myDir, fname);

    if (file.exists()) file.delete();
    try {
        out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
        out.close();
        openScreenshot(file);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Tony
  • 529
  • 2
  • 6
  • 24
  • 1
    I raised a similar question. I want to capture the whole of what is being displayed, which I think is what you are asking for as well. I found this video which might help, but it looks to me like this code will only capture the view of the current app, which would be the line strokes over a transparent background. https://www.youtube.com/watch?v=nJ5Wu4XyzbY – TenG Mar 02 '17 at 12:51
  • I also found this that is worth a try - http://stackoverflow.com/questions/2661536/how-to-programmatically-take-a-screenshot-in-android - and look for "keyevent 120". – TenG Mar 02 '17 at 12:56
  • Not getting any joy out of these two links unfortunately. – Tony Mar 02 '17 at 17:08
  • Can someone please have a look at this again, it's come back to haunt me. – Tony Dec 11 '17 at 11:15
  • Did you find any solution ? – Ahmad Dec 21 '19 at 11:05
  • Unfortunately I didn't find any solution for this. – Tony Dec 23 '19 at 12:52

0 Answers0