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

0 votes
I have to take a screenshot of the Activity which should not include the title bar and that the user should not see that the screenshot is taken following which, I need to share it using the ‘Share’ action menu button. I have tried using as many solutions as I possibly could but none are working for me. Could anyone help me with this?
Feb 8 in Others by Rahul
• 8,980 points
13 views

1 answer to this question.

0 votes

For me, I captured and then shared the screen by firstly, getting a root view from the current activity:-

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


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

}

After which, I stored the Bitmap into the SD card:- 

public static void store(Bitmap bm, String fileName){ 
      final static String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots"; 
      File dir = new File(dirPath); 
      if(!dir.exists()) 
      dir.mkdirs(); 
File file = new File(dirPath, fileName); 
try { 
      FileOutputStream fOut = new FileOutputStream(file);   bm.compress(Bitmap.CompressFormat.PNG, 85, fOut); 
fOut.flush(); 
fOut.close(); 

} catch (Exception e) { 
e.printStackTrace(); 
}
}

Finally, in order to share the screenshot of the Current Activity, I used the following lines of code:-

private void shareImage(File file){ 
   Uri uri = Uri.fromFile(file);
      Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_SEND); 
    intent.setType("image/*"); 

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(); 
    }

}

After which, do Add permissions below into your given AndroidManifest.xml:

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

This helps as it creates the files in the external storage and also allows and accesses the files.

answered Feb 8 by Soham
• 8,730 points

Related Questions In Others

0 votes
2 answers

How to get the URL of the current tab in Google Chrome?

Its so simple.... If you want to ...READ MORE

answered Aug 12, 2020 in Others by Steve
• 200 points
624 views
0 votes
1 answer

What is a name function in JavaScript & how to define it?

A named function declares a name as ...READ MORE

answered Mar 7, 2019 in Others by Frankie
• 9,830 points
703 views
0 votes
1 answer
0 votes
1 answer

Error: IllegalArgumentException: Unable to locate adb in Android Studio v2.3

when you try to move an Android project ...READ MORE

answered Dec 16, 2020 in Others by Gitika
• 65,850 points
1,651 views
0 votes
1 answer

"Default Activity Not Found" on Android Studio upgrade

If you see that ERROR occurrence after ...READ MORE

answered Feb 11 in Others by Soham
• 8,730 points
101 views
0 votes
1 answer

Invoke-customs are only supported starting with android 0 --min-api 26

After hours of working on this probleme, ...READ MORE

answered Feb 16 in Others by Soham
• 8,730 points
259 views
0 votes
1 answer
0 votes
1 answer

Media Queries: How to target desktop, tablet, and mobile?

I would personally believe that these are ...READ MORE

answered Feb 11 in Others by Soham
• 8,730 points
221 views
0 votes
1 answer

How can I easily crop a PDF page?

To answer your question, you could use ...READ MORE

answered Feb 8 in Others by Soham
• 8,730 points
19 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP