3

I have this code for take screenshot of current view, a fragment that lives into an activity, where the activity has only a background.

private File captureScreen() {

    Bitmap screenshot = null;
    try {

        if (view != null) {

            screenshot = Bitmap.createBitmap(view.getMeasuredWidth(),
                    view.getMeasuredHeight(), Config.ARGB_8888);
            Canvas canvas = new Canvas(screenshot);
            view.draw(canvas);
            // save pics
            File cache_dir = Environment.getExternalStorageDirectory();
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            screenshot.compress(Bitmap.CompressFormat.PNG, 90, bytes);
            File f = new File(cache_dir + File.separator + "screen.png");
            f.createNewFile();
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());
            fo.close();
            return f;
        }
    } catch (Exception e) {
        // TODO
    }
    return null;
}

but bitmap saved is not exactly what i'm expecting. Screenshot take only fragment elements, but not activity background. How can i include it into screenshot?

giozh
  • 9,868
  • 30
  • 102
  • 183
  • 1
    I suggest you to get help from this question. http://stackoverflow.com/questions/2661536/how-to-programatically-take-a-screenshot-on-android – Umair Khalid Apr 01 '15 at 08:40

2 Answers2

0

From :How to programmatically take a screenshot in Android?

// image naming and path  to include sd card  appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;   
// create bitmap screen capture
Bitmap bitmap;
View v1 = mCurrentUrlMask.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

OutputStream fout = null;
imageFile = new File(mPath);

try {
  fout = new FileOutputStream(imageFile);
  bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
  fout.flush();
  fout.close();

} catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
} catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

try this. it work for me. and for you too

Community
  • 1
  • 1
Umair Khalid
  • 2,259
  • 1
  • 21
  • 28
0

Call this method, passing in the outer most ViewGroup that you want a screen shot of:

public Bitmap screenShot(View view) {
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
            view.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    return bitmap;
}

I've used it for a while in a few different apps and haven't had any issues. Hope it vll helps

King of Masses
  • 18,405
  • 4
  • 60
  • 77