SM2011
SM2011

Reputation: 287

Programmatically crop Android screen grab?

I'm using the following (scavenged) code to take a 'screenshot' and action_send it. It's al lworking okay, the only problem is the image doesn't contain the status bar. I know why, I'm just not sure what to do about it. Is there any way I can crop the image programmatically, or set some kind of boundary before I make the getDrawingCache() call??

Thanks in advance!

View v1 = findViewById(android.R.id.content).getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap screenshot = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

String path = Images.Media.insertImage(getContentResolver(), screenshot, "title", null);
Uri screenshotUri = Uri.parse(path);

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);

startActivity(Intent.createChooser(sharingIntent, "Share image using"));

Upvotes: 5

Views: 2878

Answers (2)

cjk
cjk

Reputation: 46465

The problem is that you aren't taking a screenshot as such, you are getting the current image of your application. This cannot include the status bar as it is not part of your application, nor can you extend the drawing area upwards to capture the status bar.

Upvotes: 0

Parth Doshi
Parth Doshi

Reputation: 4208

You can try and use Bitmap.createBitmap on your screenshot before you send it.

Bitmap cropimg = Bitmap.createBitmap(originalImg, 0, 0, originalImg.width(), originalImg.height() - x)

'x' which is an Integer is the number of pixels that are cropped from the bottom of the image in this case. You can do certain modifications and crop accordingly to suit your requirement.

Hope it helps

Upvotes: 3

Related Questions