0

I am developing an android app and I'am trying to make the following code works. What i want to do is: taking screenshot of the whole activity including the text that is not being shown (have to scroll up or down). This is the screenshot method:

   public static Bitmap takeScreenshot(Activity activity){
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    Bitmap bmap = view.getDrawingCache();

    Rect statusBar = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(statusBar);
    Bitmap snapshot = Bitmap.createBitmap(bmap, 0, statusBar.top, bmap.getWidth(), bmap.getHeight() - statusBar.top, null, true);

    view.setDrawingCacheEnabled(false);
    return snapshot;
}
public void saveBitmap(Bitmap bitmap) {
    File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}

I want to call takeScreenshot on the following function, but i don't know how to pass an activity in takeScreenshot's parameter. Ive tried to copy the name of the activity and it didn't work.

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detailed__info);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    Intent intent = getIntent();

     id = intent.getStringExtra(Urls.MARKER_ID);
    year = intent.getStringExtra(Urls.MARKER_Year);
    info = intent.getStringExtra(Urls.MARKER_Info);

    editTextName = (TextView) findViewById(R.id.markerYear);
    editTextDesg = (TextView) findViewById(R.id.detailedInfo);


    editTextName.setText(year);
    editTextDesg.setText(info);


    //Saving into picture

    findViewById(R.id.download).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bitmap bitmap = takeScreenshot(); // here's where i have to pass the activity
            saveBitmap(bitmap);
        }
    });
}
bhk33
  • 11
  • 2

3 Answers3

0

Passing Activity is same as you pass the context of the current activity. You just need to this within your Activity:

takeScreenshot(this)
Mayank Bhatnagar
  • 2,120
  • 1
  • 12
  • 20
  • `this` will represent not activity in this case, because it was inside `onClick` – Pragnani Feb 26 '16 at 11:58
  • In that case this would be referenced with your Activity. takeScreenshot(Activity.this) Replace Activity with your Activity name. – Mayank Bhatnagar Feb 26 '16 at 12:40
  • I am not OP, I have corrected your answer.. I have tried to correct you instead of giving downvote – Pragnani Feb 26 '16 at 12:46
  • t's not working it seems like when i click on the button to screenshot nothing is happened. is there something wrong with the function takeScreenshot? – bhk33 Feb 26 '16 at 13:08
0

Just pass the activity context in the method.

Bitmap bitmap = takeScreenshot(MyActivity.this); would work for you.

Update

public static Bitmap takeScreenshot(Activity activity) {
    try {
        // create bitmap screen capture
        View v1 = activity.getWindow().getDecorView().getRootView();

        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        return bitmap;

    } catch (Throwable e) {
        // Several error may come out with file handling or OOM
        e.printStackTrace();
    }
    return null;
}
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • It's not working it seems like when i click on the button to screenshot nothing is happened. is there something wrong with the function takeScreenshot? – bhk33 Feb 26 '16 at 13:08
  • @bhk33: I have update my answer with what I am using. See if that works – Rohit5k2 Feb 26 '16 at 13:14
  • @bhk33 You can see my answer for taking screnshot here http://stackoverflow.com/questions/35430817/how-to-programmatically-take-a-screenshot-in-android-of-alert-dialog/35432168#35432168 – Rohit5k2 Feb 26 '16 at 13:14
0

Try this for capturing screenshot:

public static Bitmap captureScreen(View v) {

        Bitmap screenshot = null;
        try {

            if(v!=null) {

                screenshot = Bitmap.createBitmap(v.getMeasuredWidth(),v.getMeasuredHeight(), Config.ARGB_8888);
                Canvas canvas = new Canvas(screenshot);
                v.draw(canvas);
            }

        }catch (Exception e){
            Log.d("ScreenShotActivity", "Failed to capture screenshot because:" + e.getMessage());
        }

        return screenshot;
    }

    public static void saveImage(Bitmap bitmap) throws IOException{

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 40, bytes);
        File f = new File(Environment.getExternalStorageDirectory() + File.separator + "test.png");
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        fo.close();
    }

For further reference visit the below link:

http://karanbalkar.com/2014/03/get-screenshot-of-device-screen-in-android/

Mayank Bhatnagar
  • 2,120
  • 1
  • 12
  • 20
  • Does this capture the entire screen including the text that is not being shown (have to scroll up or down)? – bhk33 Feb 27 '16 at 09:22