Я хотел бы поделиться снимком экрана после того, как пользователь нажал кнопку «поделиться».Я пытаюсь применить это решение: https://stackoverflow.com/a/30212385/9748825
Вот три метода:
public static Bitmap getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
return bitmap;
}
public void store(Bitmap bm, String fileName) {
final 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();
}
Toast.makeText(this, file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
shareImage(file);
}
public void shareImage(File file) {
Uri uri = FileProvider.getUriForFile(iv_ScoreBoard.this, iv_ScoreBoard.this.getApplicationContext().getPackageName() + ".my.package.name.provider", file);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
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(iv_ScoreBoard.this, "No App Available", Toast.LENGTH_SHORT).show();
}
}
Метод gameDate:
public void generateNewGameDate() {
Date thisSecond = Calendar.getInstance().getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd , HH:mm");
gameDate = df.format(thisSecond);
}
Точка запуска:
public void onClick(View v) {
Bitmap b = getScreenShot(rootView);
store(b, gameDate);
}
Ошибка:
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Screenshots/2018-10-28 , 17:40
Благодарим Вас за любую помощь, спасибо!