安卓 - 无法截取当前屏幕的截图

12

对于第一张屏幕截图,代码能够正常工作,并且无论移动到另一个视图,它仍然保持获取相同的截图。

如何获取当前的屏幕截图?

public void saveBitmap(Bitmap bitmap) {

    File imagePath = new File(Environment.getExternalStorageDirectory() + "/" + new SimpleDateFormat("yyyyMMddhhmmss'.jpg'").format(new Date()) );
    FileOutputStream fos =null;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}

点击信息:

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.iSave:
          Bitmap bitmap = null;
          bitmap = takeScreenshot();
          saveBitmap(bitmap);
        break;
    } 
}

这里:

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

1
请发布 takeScreenshot() 方法。 - Rajat Mehra
发布于.. @rajatmehra - Fou
尝试使用getWindow().getDecorView().getRootView()代替findViewById(android.R.id.content).getRootView()。 - Rajat Mehra
如果返回到主界面并重新进入此活动,则可以正常工作。 - Fou
以上代码可以截屏,但在第一次点击后,重复操作会得到相同的截图,无论更改了什么。 - Fou
@Vikram还是不行,仍然一样... - Fou
2个回答

15

在截屏后调用rootView.setDrawingCacheEnabled(false);。关闭它然后再打开可以强制更新并解决问题。

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

2

我曾试图捕获当前Activity并分享截图。以下是我的做法,如果您还感兴趣,可以看一下,我想您会同意的。

首先,获取当前Activity的根视图:

View rootView = getWindow().getDecorView().findViewById(android.R.id.content);

或者

View rootView = findViewById(android.R.id.content);

或者

View rootView = findViewById(android.R.id.content).getRootView();

第二步,从根视图获取Bitmap

public static Bitmap getScreenShot(View view) {
    View screenView = view.getRootView();
    screenView.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
    screenView.setDrawingCacheEnabled(false);
    return bitmap;
}

第三步,将Bitmap存储到SDCard中:

private final static String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots";
public static void store(Bitmap bm, String fileName){
    File dir = new File(dir);
    if(!dir.exists())
        dir.mkdirs();
    File file = new File(dir, fileName);
    try {
        FileOutputStream fOut = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
        fOut.flush();
        fOut.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

最后,分享截图文件:
private void shareImage(String file){
    Uri uri = Uri.fromFile(file);
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.setType("image/*");
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
    intent.putExtra(android.content.Intent.EXTRA_TEXT, "");
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(intent, "Share Screenshot"));
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接