How to take a program screenshot and save it in the gallery?

I would like to know what the code is to take a screenshot of the current screen (after pressing the button) and save it in the gallery, because I do not have a device with SD cards. Therefore, I would like to keep the default gallery. thank

+5
source share
4 answers
  Bitmap bitmap;
  View v1 = findViewById(R.id.rlid);// get ur root view id
  v1.setDrawingCacheEnabled(true); 
  bitmap = Bitmap.createBitmap(v1.getDrawingCache());
  v1.setDrawingCacheEnabled(false);

That should do the trick.

To save

  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();
+9
source
    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 the complete source code, follow the blog below.

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

To save a bitmap to see the link below

Android Save created raster image to directory on SD card

+4
source

This will be saved in the gallery. The code also sets the image path, which is useful with Intent.SEND_ACTION and email settings.

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;
}
+1
source

As noted in 323go, this is not possible if your device is not deployed.

But if so, this might be a good job for monkeyrunner or if you are using an emulator.

0
source

All Articles