【问题标题】:Share screen shoot android studio(multi-times)分享屏幕截图android studio(多次)
【发布时间】:2017-04-27 12:36:02
【问题描述】:

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

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

** 我的每张图片都有不同的文件名,但共享总是采取最后一个未完成的操作。 (如果用户共享所有内容将正常工作,下一个屏幕截图将是新的)

这是我的代码

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

【问题讨论】:

  • 你好,我只是想问你关于截图部分,这段代码是截取你设备的布局还是主屏幕?
  • 这个问题只发生在 facebook 上吗?
  • 截取新的截图就删掉就好了。
  • @Mousa,所有的布局,取决于你把它放在哪里,(活动的意思)
  • @layth ,嗯...,谢谢,但我正在尝试对设备的主屏幕进行屏幕截图。

标签: java android share screenshot


【解决方案1】:

您可以这样做。从图像名称中删除 System.currentTimeMillis(),这样您就没有多个屏幕截图副本。因此,当您上传图像时,它总是有新的屏幕截图。现在当您捕获另一个屏幕截图,您必须检查文件是否存在(如果存在)然后将其删除。

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

【讨论】:

  • 我现在就试试
  • 同样的问题 :(
  • 哦,这怎么可能。在这个代码中,当你捕获新的图像时,图像被删除。那么怎么可能得到新的。请确保截图的路径。
  • 使用你的方式的问题是应用程序无法删除旧图像,我已经添加了这些权限>>>>>
  • 删除图像时出现任何错误?无论是否进入该条件,请在 if 条件下打印日志。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-05
  • 1970-01-01
  • 1970-01-01
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多