安卓截图?
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 的空白文件?
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();
}
}
});
above code is to capture a screenshot, but it creates blank file of zero kb?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
file.createNewFile();
完全按照您的要求执行:创建一个空白的空文件。然后,如果绘图缓存为空,您将在文件中得不到任何内容。试试这个:file.createNewFile();
does exactly what you asked of it: creates a blank, empty file. Then if the drawing cache is empty you will get nothing in the file. Try this: