Share screen shoot android studio(multi-times)

。_饼干妹妹 提交于 2019-12-13 03:41:12

问题


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);
    }
}


来源:https://stackoverflow.com/questions/43657993/share-screen-shoot-android-studiomulti-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!