I am making an application in Android in which I have to take screenshot of one of my activities and mail it as attachment.
I want to take screenshot of the current page and then share it via email, Bluetooth, Twitter or Facebook.
My code is as follows:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menuselected1, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.ScreenShot:
try {
takeScreenShot(this);
}
catch (Exception e) {
System.out.println(e);
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private static void savePic(Bitmap b, String strFileName) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(strFileName);
if (null != fos) {
b.compress(Bitmap.CompressFormat.PNG, 90, fos);
System.out.println("b is:"+b);
fos.flush();
fos.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void shoot(Activity a,String b) {
//savePic(takeScreenShot(a), "sdcard/xx.png");
savePic(takeScreenShot(a), b);
}
private static Bitmap takeScreenShot(Activity activity) {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay()
.getHeight();
// Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
- statusBarHeight);
view.destroyDrawingCache();
return b;
}
If by "screenshot of current page" you mean "screenshot of one of my activities", you can arrange to render your
Views
to a bitmap-backedCanvas
, then save an image from the bitmap.If by "screenshot of current page" you mean "screenshot of somebody else's activity", that is not supported by the Android SDK, for obvious privacy and security reasons. There are various techniques that rooted device users can use to take screenshots.
Try this for taking screenshot of current Activity:
Android 2.2 :
This is how I captured the screen and shared it. Take a look if you are interested.
And the method that saves the bitmap image to external storage:
see more in : https://www.youtube.com/watch?v=LRCRNvzamwY&feature=youtu.be
1. Create the share button
I wanted mine in the action bar so I created a share_menu.xml file:
This adds a button in the action bar with my share_icon and the text.
2. Add the sharing menu to your activity (or fragment)
I did this inside a fragment so I added the code below to my fragment file. If you are inside an activity then you override
public boolean onCreateOptionsMenu(Menu menu)
instead.if you are doing this with a fragment then in
onCreate()
you have to add:3. Set up button action/callback/onclick
This is what is going to kick off the sharing.
Notice this calls two magic methods:
screenShot():
Important
To your
AndroidManifest.xml
, you have to add:or the screenshot won't be saved, and gmail will think you are trying to attach an empty file.
Also, a lot of SO answers say to use
"*/*"
forshareIntent.setType()
but this creates an issue with facebook sharing, so it's best to leave it as"image/*"
.