For those who may need a solution in the future, this is what I use in my applications. Tested and works on Android 4.0 to 7.1.
First, define the variable for the visible screen in your Office:
public View preView;
Then run a view to allow a screenshot:
preView = getWindow().getDecorView();
Then you can call the next method in your activity by pressing the button. I put this method in a headless class and refer to it as needed.
public static void takeScreenShotAndShare(final Context context, View view, boolean incText, String text){
try{
File mPath = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "screenshot.png");
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
FileOutputStream fOut = new FileOutputStream(mPath);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.PNG, quality, fOut);
fOut.flush();
fOut.close();
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri pictureUri = Uri.fromFile(mPath);
shareIntent.setType("image/*");
if(incText){
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
}
shareIntent.putExtra(Intent.EXTRA_STREAM, pictureUri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(shareIntent, "Share image using"));
}catch (Throwable tr){
Log.d(TAG, "Couldn't save screenshot", tr);
}
}
source
share