找到你要的答案

Q:Android - Not taking screenshot of current screen

Q:Android -不采取截图当前屏幕

Code work fine for first screenshot and keep taking same screenshot regardless of moving to another view.

How to get current screenshot?

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

click info:

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

here:

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

代码工作罚款第一截图和保持相同的截图,无论移动到另一个视图。

如何获得当前截图?

public void saveBitmap(Bitmap bitmap) {

    File imagePath = new File(Environment.getExternalSt或ageDirect或y() + "/" + new SimpleDateF或mat("yyyyMMddhhmmss'.jpg'").f或mat(new Date()) );
    FileOutputStream fos =null;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(CompressF或mat.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();
}
answer1: 回答1:

Call rootView.setDrawingCacheEnabled(false); after taking the screen-shot. Turning it off and then on again forces it to update correctly.

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

调用RootView。setdrawingcacheenabled(假);以画面后。关闭它,然后再次迫使它正确更新。

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

I have ever tried to capture the current Activity and then share the screenshot. This below is how I did, take a look at them if you are still interested, and I think you would agree.

First, the get the root view of current Activity:

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

or

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

or

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

Second, get Bitmap from the root view:

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

Third, store the Bitmap into the 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();
    }
}

At last, share the screenshot file:

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

我曾经试图捕捉当前的活动,然后分享截图。下面是我做的,如果你还感兴趣的话看看他们,我想你会同意的。

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

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

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

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

第二,从根视图获取位图:

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

Third, st或e the Bitmap into the SDCard:

private final static String dir = Environment.getExternalSt或ageDirect或y().getAbsolutePath() + "/Screenshots";
public static void st或e(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.CompressF或mat.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"));
}
android