2011-06-22 98 views
-2
 Button button = (Button)findViewById(R.id.btnTakeScreenshot); 
     button.setOnClickListener(new View.OnClickListener() { 
     //@Override 
     public void onClick(View v) { 
     final View content = findViewById(R.id.layoutroot); 
     content.setDrawingCacheEnabled(true); 
     Bitmap bitmap = content.getDrawingCache(); 
     File file = new File(Environment.getExternalStorageDirectory() + "/test.png"); 
     try 
     { 
      file.createNewFile(); 
      FileOutputStream ostream = new FileOutputStream(file); 
      bitmap.compress(CompressFormat.PNG, 100, ostream); 
      ostream.close(); 
      Toast.makeText(content.getContext(), "HELLO", Toast.LENGTH_SHORT); 
     } 
     catch (Exception e) 
     { 
     System.out.print("error"); 
      e.printStackTrace(); 
     } 
} 
}); 

上面的代碼是捕獲一個截圖,但它創建零kb的空白文件?android截圖?

+0

它會拋出任何異常嗎? – Egor

+0

不...它不會拋出任何異常...問題是在保存/捕獲圖像.. – alpesh

+0

它不會出現,如果你自己試圖解決這個問題。 –

回答

2

file.createNewFile();完全符合您的要求:創建一個空白的空文件。然後,如果繪圖緩存爲空,則文件中將不會顯示任何內容。試試這個:

Button button = (Button)findViewById(R.id.btnTakeScreenshot); 
     final View content = findViewById(R.id.layoutroot); 
     content.setDrawingCacheEnabled(true); 
     button.setOnClickListener(new View.OnClickListener() { 
     //@Override 
     public void onClick(View v) { 
     Bitmap bitmap = content.getDrawingCache(); 
     File file = new File(Environment.getExternalStorageDirectory() + "/test.png"); 
     try 
     { 
      file.createNewFile(); 
      FileOutputStream ostream = new FileOutputStream(file); 
      bitmap.compress(CompressFormat.PNG, 100, ostream); 
      ostream.close(); 
      Toast.makeText(content.getContext(), "HELLO", Toast.LENGTH_SHORT); 
     } 
     catch (Exception e) 
     { 
     System.out.print("error"); 
      e.printStackTrace(); 
     } 
} 
}); 
+0

嘿人...它的工作.....好解決費米... thanku所以muh – alpesh