分享屏幕拍摄android studio(多次) [英] Share screen shoot android studio(multi-times)

查看:60
本文介绍了分享屏幕拍摄android studio(多次)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有应用程序,我让用户制作屏幕截图并分享它,一切正常,只有一个问题..

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

例如当用户截图并按下Facebook图标时>然后他按下取消分享,下次当他想再次分享时,他会看到旧的屏幕截图,如何让它总是截取最后一张截图??

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 )

这是我的代码

 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, "مشاركة بواسطة"));
 }

推荐答案

您可以这样做.从您的图像名称中删除 System.currentTimeMillis(),这样您就不会拥有多个屏幕截图副本.所以当你上传图片时,它总是有新的截图.现在当你捕捉另一个截图时,你必须检查文件是否存在,如果存在,然后删除它.

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

这篇关于分享屏幕拍摄android studio(多次)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆