2015-05-04 61 views
12

Mã hoạt động tốt cho ảnh chụp màn hình đầu tiên và tiếp tục chụp cùng một ảnh chụp màn hình bất kể chuyển sang chế độ xem khác.Android - Không chụp màn hình của màn hình hiện tại

Cách tải ảnh chụp màn hình hiện tại?

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

thông tin nhấp chuột:

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

đây:

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

bài takeScreenshot() phương pháp. –

+0

posted .. @rajatmehra – Fou

+0

Thử getWindow(). GetDecorView(). GetRootView() thay vì findViewById (android.R.id.content) .getRootView(). –

Trả lời

15

Gọi rootView.setDrawingCacheEnabled(false); sau khi màn hình-shot. Tắt rồi bật lại để cập nhật chính xác.

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

hoạt động nhờ .....;) – Fou

2

Tôi đã từng chụp ảnh Activity hiện tại và sau đó chia sẻ ảnh chụp màn hình. Dưới đây là cách tôi đã làm, hãy xem chúng nếu bạn vẫn còn quan tâm, và tôi nghĩ rằng bạn sẽ đồng ý.

Đầu tiên thì có được xem gốc của hiện tại Activity:

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

hoặc

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

hoặc

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

Second, có Bitmap từ thứ e xem gốc:

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

Thứ ba, lưu trữ các Bitmap vào 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, chia sẻ các tập tin ảnh chụp màn hình:

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")); 
} 
Các vấn đề liên quan