2015-05-04 49 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")); 
}