5

i would to know what is the code to take a screenshot of the current screen (after a press of a button) and save it on a gallery because I don't have a device with sd cards. So i would to save in the default gallery. thank you

penny89
  • 173
  • 1
  • 2
  • 12
  • It's not possible unless your device is rooted. – 323go Mar 07 '13 at 16:24
  • try this one..... [http://stackoverflow.com/questions/7762643/android-take-screen-shot-programatically][1] [1]: http://stackoverflow.com/questions/7762643/android-take-screen-shot-programatically – Melbourne Lopes Apr 08 '14 at 16:54

4 Answers4

10
  Bitmap bitmap;
  View v1 = findViewById(R.id.rlid);// get ur root view id
  v1.setDrawingCacheEnabled(true); 
  bitmap = Bitmap.createBitmap(v1.getDrawingCache());
  v1.setDrawingCacheEnabled(false);

This should do the trick.

For saving

  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
  File f = new File(Environment.getExternalStorageDirectory()
                    + File.separator + "test.jpg")
  f.createNewFile();
  FileOutputStream fo = new FileOutputStream(f);
  fo.write(bytes.toByteArray()); 
  fo.close();
Raghunandan
  • 129,147
  • 24
  • 216
  • 249
5
    View v1 = L1.getRootView();
    v1.setDrawingCacheEnabled(true);
    Bitmap bm = v1.getDrawingCache();
    BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
    image = (ImageView) findViewById(R.id.screenshots);
    image.setBackgroundDrawable(bitmapDrawable);

For complete source code go through the below blog

http://amitandroid.blogspot.in/2013/02/android-taking-screen-shots-through-code.html

For storing the Bitmap to see the below link

Android Saving created bitmap to directory on sd card

Community
  • 1
  • 1
Amit Gupta
  • 8,604
  • 1
  • 22
  • 32
1

This will save to the gallery. The code also sets an image path.. that is useful with Intent.SEND_ACTION and email Intents.

String imagePath = null;
Bitmap imageBitmap = screenShot(mAnyView);
if (imageBitmap != null) {
    imagePath = MediaStore.Images.Media.insertImage(getContentResolver(), imageBitmap, "title", null);
}


public Bitmap screenShot(View view) {
    if (view != null) {
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
                view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
        return bitmap;
    }
    return null;
}
Lee Hounshell
  • 724
  • 6
  • 9
  • I know this is a few months old, but it sort of worked ... except ... it captured the contents of the view but NOT the subviews. How do you get a bitmap of everything that is displayed on top of the view (aka, the entire screen)? – ByteSlinger Feb 18 '18 at 00:14
0

As 323go commented, this isn't possible unless your device is rooted, really.

But if it is, it might be a good job for monkeyrunner or if you're using an emulator.

poitroae
  • 20,146
  • 9
  • 56
  • 77