将表面视图保存到画布
我正在尝试保存我的应用程序的屏幕截图。 我的主屏幕是 SurfaceView,我正在创建一个新画布并将 SurfaceView 绘制到画布中。我有一个问题,因为我得到的 PNG 是完全透明的。
这是我的代码
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(image);
draw(c);
String path=Environment.getExternalStorageDirectory() + "/test2.png";
File file = new File(path);
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
image.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
Uri screenshotUri = Uri.parse("file://"+file.getAbsolutePath());
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setDataAndType(screenshotUri, "image/png");
startActivity(sendIntent);
}
catch (Exception e)
{
e.printStackTrace();
}
I'm trying to save a screenshot of my app.
My main screen is a SurfaceView, I'm creating a new canvas and drawing the surfaceview into the canvas. I have a problem because the PNG I get is totally transparent.
Here's my code
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(image);
draw(c);
String path=Environment.getExternalStorageDirectory() + "/test2.png";
File file = new File(path);
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
image.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
Uri screenshotUri = Uri.parse("file://"+file.getAbsolutePath());
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setDataAndType(screenshotUri, "image/png");
startActivity(sendIntent);
}
catch (Exception e)
{
e.printStackTrace();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要通过这样做来确保表面已完全创建
http://www.edu4java .com/androidgame/androidgame3.html
尝试在 onSurfaceCreated 方法中截取屏幕截图,否则在表面存在之前您将得到空白。t 文档说在渲染完整布局之前,draw() 将不会返回任何内容。
I think you need to ensure that the surface has been fully created by doing this
http://www.edu4java.com/androidgame/androidgame3.html
Try taking your screenshot in the onSurfaceCreated method or else you will get blank until the surface exists.t The documentation says draw() will return nothing until a full layout has been rendered.