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, 2022 in Others by Rahul
• 9,670 points
89 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, 2022 by Soham
• 9,700 points

Related Questions In Others

0 votes
1 answer
0 votes
1 answer

Using excel I need to open PPT and create ".gif" image of a ."pdf" and save it

It appears happier if you get a ...READ MORE

answered Dec 24, 2022 in Others by narikkadan
• 50,080 points
49 views
0 votes
1 answer

VBA How to extract the date and time of arrival of a answered email

Use MailItem.ReceivedTime property. I hope this helps you ...READ MORE

answered Jan 9 in Others by narikkadan
• 50,080 points
33 views
0 votes
1 answer

VBA How to extract the date and time of arrival of a answered email

Use MailItem.ReceivedTime property. I hope this helps you ...READ MORE

answered Jan 9 in Others by narikkadan
• 50,080 points
43 views
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,910 points
1,842 views
–1 vote
1 answer

"Default Activity Not Found" on Android Studio upgrade

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

answered Feb 11, 2022 in Others by Soham
• 9,700 points
454 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, 2022 in Others by Soham
• 9,700 points
1,317 views
0 votes
1 answer

Execution Failed for task :app:compileDebugJavaWithJavac in Android Studio

In Android Studio 3.1, you can see ...READ MORE

answered Feb 17, 2022 in Java by Aditya
• 7,660 points
6,061 views
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, 2022 in Others by Soham
• 9,700 points
1,984 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, 2022 in Others by Soham
• 9,700 points
495 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP