just_deko just_deko - 1 year ago 111
Android Question

How to take a screenshot of current Activity and then share it?

I need to take a screenshot of

(without the title bar, and the user should NOT see that a screenshot has actually been taken) and then share it via an action menu button "share". I have already tried some solutions, but they didn't work for me. Any ideas?

Answer Source

This is how I captured the screen and shared it. Take a look if you are interested.

First, get root view from current activity:

View rootView = getWindow().getDecorView().findViewById(android.R.id.content);

Second, capture the root view:

 public static Bitmap getScreenShot(View view) {
       View screenView = view.getRootView();
       Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
       return bitmap;

Third, store the Bitmap into the SDCard:

public static void store(Bitmap bm, String fileName){
    final static String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots";
    File dir = new File(dirPath);
    File file = new File(dirPath, fileName);
    try {
        FileOutputStream fOut = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
    } catch (Exception e) {

At last, share the screenshot of current Activity:

private void shareImage(File file){
    Uri uri = Uri.fromFile(file);
    Intent intent = new Intent();

    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
    intent.putExtra(android.content.Intent.EXTRA_TEXT, "");
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    try {
        startActivity(Intent.createChooser(intent, "Share Screenshot"));
    } catch (ActivityNotFoundException e) {
        Toast.makeText(context, "No App Available", Toast.LENGTH_SHORT).show();

I hope you will be inspired by my codes.


Add below permissions into your AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Because it creates and accesses files in external storage.