2011-06-22 58 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