Share screen shoot android studio(multi-times)

2019-09-16 04:32发布

问题:

I have app and i am letting user to make screenshot and share it, all working fine just one problem..

When user for example make screenshot and press on Facebook icon > then he press cancel sharing ,next time when he want to do another share he will see the old screen shoot , how can make it take the last screenshot always??

** i have each image has different file name but the share always taking the last action that didn't finish. (if user do share all will work fine ,next screen shoot will be the new one )

here is my code

 public Bitmap takeScreenshot() {
    View rootView = findViewById(android.R.id.content).getRootView();
    rootView.setDrawingCacheEnabled(true);
    return rootView.getDrawingCache();
  }


 public void saveBitmap(Bitmap bitmap) {
    imagePath = new File(Environment.getExternalStorageDirectory() , 
"SCREEN"
            + System.currentTimeMillis() + ".png");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
  }



 private void shareIt() {
    uri =Uri.fromFile(imagePath);
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("image/*");
    String shareBody = "جرب تطبيق نكت عراقية مضحكة الان!";
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "تطبيق نكت 
 عراقية مضحكة");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
    sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);

    startActivity(Intent.createChooser(sharingIntent, "مشاركة بواسطة"));
 }

回答1:

You can do something like this.Remove the System.currentTimeMillis() from the name of your image so, that you do not have multiple copies of screenshots.So when you upload the image it always have the fresh screenshots.Now when you capture another screenshot you have to check is file exist if exist then delete it.

public void saveBitmap(Bitmap bitmap)
{
    File file = new File(Environment.getExternalStorageDirectory(), "SCREEN.png");
    if (file.exists()) {
        file.delete();
    }
    imagePath = new File(Environment.getExternalStorageDirectory() , "SCREEN.png");

    FileOutputStream fos;
    try
    {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    }
    catch (IOException e)
    {
        Log.e("GREC", e.getMessage(), e);
    }
}