I want the application in the background to be able to take a screenshot and save the result to the clipboard. Is there an optimal solution to this problem?
Asked
Active
Viewed 71 times
-1
-
Try the suggested answers for [taking a screenshot using a background service](https://stackoverflow.com/a/47097901/16653700) and [taking a screenshot programmatically](https://stackoverflow.com/a/5651242/16653700). – Alias Cartellano Jan 08 '22 at 17:21
1 Answers
0
You can use the following code examples
public class HelperScreenShot {
public static Bitmap takeScreenshot(View v) {
v.setDrawingCacheEnabled(true);
v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
return b;
}
public static Bitmap takeScreenshotOfRootView(View v) {
return takeScreenshot(v.getRootView());
}
public static boolean takeScreenshotAndSaveIi(View v, String filename) {
return storeScreenshot(takeScreenshot(v.getRootView()), filename);
}
public static boolean storeScreenshot(Bitmap bitmap, String filename) {
if (!isExternalStorageReadable()) {
return false;
}
if (!isExternalStorageWritable()) {
return false;
}
OutputStream out;
try {
File dir = getDownloadStorageDir("ScreenShots");
File imageFile = new File(dir, filename + ".jpg");
if (!imageFile.exists()) {
imageFile.createNewFile();
}
out = new FileOutputStream(imageFile);
// choose JPEG format
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
MediaScannerConnection.scanFile(G.context, new String[]{imageFile.getAbsolutePath()}, null, null);
out.flush();
out.close();
return true;
} catch (FileNotFoundException e) {
// manage exception ...
return false;
} catch (IOException e) {
// manage exception ...
return false;
}
}
/* Checks if external storage is available for read and write */
private static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state);
}
/* Checks if external storage is available to at least read */
private static boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
}
private static File getDownloadStorageDir(String fileName) {
// Get the directory for the user's public pictures directory.
File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), fileName);
if (!Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).exists()) {
new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath()).mkdirs();
}
if (!storageDir.exists()) {
storageDir.mkdir();
}
return storageDir;
}
}
private Single<Boolean> loadImage() {
Date date = new Date();
filename = "receipt" + date.getTime();
return Single.just(HelperScreenShot.takeScreenshotAndSaveIi(binding.v, filename));
}
private void getScreenshot() {
loadImage()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<Boolean>() {
@Override
public void onSubscribe(@io.reactivex.annotations.NonNull Disposable d) {
disposables.add(d);
}
@Override
public void onSuccess(@io.reactivex.annotations.NonNull Boolean s) {
if (s) {
Snackbar snackbar = Snackbar.make(binding.v, getResources().getString(R.string.picture_save_to_galary), Snackbar.LENGTH_LONG);
snackbar.setAction(getResources().getString(R.string.navigation_drawer_open), new View.OnClickListener() {
@Override
public void onClick(View v) {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "ScreenShots/" + filename + ".jpg");
Log.d("amini", "onClick: " + file.getAbsolutePath());
Intent intent = new Intent(Intent.ACTION_VIEW).setDataAndType(
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ?
FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".provider", file) :
Uri.fromFile(file), "image/*")
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
});
snackbar.show();
} else {
Snackbar snackbar = Snackbar.make(binding.v, getResources().getString(R.string.str_frag_sync_error), Snackbar.LENGTH_LONG);
snackbar.setAction(getResources().getString(R.string.ok), v -> snackbar.dismiss());
snackbar.show();
}
}
@Override
public void onError(@io.reactivex.annotations.NonNull Throwable e) {
}
});
}

Asghar Hosseini
- 239
- 1
- 5
- 14