Создайте имя класса TakeScreenShot
//method to create a bitmap
public static Bitmap getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
return bitmap;
}
//method to share an image
public static void shareScreen(Bitmap bitmap, Activity activity, String nameChannel) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(activity.getContentResolver(), bitmap, "Title", null);
Uri imageUri = Uri.parse(path);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("image/*");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, activity.getString(R.string.app_name));
intent.putExtra(android.content.Intent.EXTRA_TEXT, "Watch " + nameChannel + " Channel From " + activity.getString(R.string.app_name)
+ " Download now from google play" + " http://play.google.com/store/apps/details?id=" + activity.getPackageName());
intent.putExtra(android.content.Intent.EXTRA_STREAM, imageUri);
try {
activity.startActivity(Intent.createChooser(intent, "Share Screenshot"));
} catch (ActivityNotFoundException e) {
Toast.makeText(activity, "No App Available", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Log.e("Error on sharing", e + " ");
Toast.makeText(activity.getApplicationContext(), "App not Installed", Toast.LENGTH_SHORT).show();
}
}
// Check permissions
public static boolean checkPermission(String permission, Activity activity) {
int check = ContextCompat.checkSelfPermission(activity, permission);
return (check == PackageManager.PERMISSION_GRANTED);
}
// Take permissions
public static void takePermission(Activity activity) {
String[] arrayPermission = new String[]{Manifest.permission.INTERNET
, Manifest.permission.WRITE_EXTERNAL_STORAGE
, Manifest.permission.READ_EXTERNAL_STORAGE};
ActivityCompat.requestPermissions(activity, arrayPermission, Constants.PERMISSION_CODE);
}
на кнопке создания вашего класса:
imgBtnShare.setOnClickListener(view -> {
if (TakeScreenShot.checkPermission(Manifest.permission.INTERNET,this)
|| TakeScreenShot.checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE,this)
|| TakeScreenShot.checkPermission(Manifest.permission.READ_EXTERNAL_STORAGE,this)) {
TakeScreenShot.shareScreen(TakeScreenShot.getScreenShot(lyBackground),this,channelModel.getChannelName());
}else {
TakeScreenShot.takePermission(this);
}
});