2015-05-04 83 views
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()方法。 –

+0

张贴.. @rajatmehra – Fou

+0

尝试getWindow()。getDecorView()。getRootView()而不是findViewById(android.R.id.content).getRootView()。 –

回答

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; 
} 
+0

工作感谢.....;) – Fou

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

At last,共享文件的屏幕截图:

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