1

I would like to share a screenshot after user clicking "share" button. I am trying to apply this solution: https://stackoverflow.com/a/30212385/9748825

Here are the three methods:

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 method:

 public void generateNewGameDate() {
    Date thisSecond = Calendar.getInstance().getTime();
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd , HH:mm");
    gameDate = df.format(thisSecond);

}

Trigger Point:

public void onClick(View v) { 
Bitmap b = getScreenShot(rootView);
store(b, gameDate);
}

Error:

java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Screenshots/2018-10-28 , 17:40

Appreciate any of your help, thanks!

1 Answers1

0

Well I was stuck in external storage permission.

After applying this answer, I got it now. Appreciate stackoverflow so much! https://stackoverflow.com/a/37672627/9748825