拍摄surface view在android中不工作的屏幕截图
在我的android应用程序中,我有一个表面视图,上面有画布,用户可以在画布上画画。现在我想捕捉画布图像并将其存储到sd卡上。 以下是我的密码-拍摄surface view在android中不工作的屏幕截图,android,android-canvas,surfaceview,surface,Android,Android Canvas,Surfaceview,Surface,在我的android应用程序中,我有一个表面视图,上面有画布,用户可以在画布上画画。现在我想捕捉画布图像并将其存储到sd卡上。 以下是我的密码- Bitmap bitmap = Bitmap.createBitmap(maxX, maxY, Bitmap.Config.RGB_565); canvas.setBitmap(bitmap); String mFile = path+"/drawing.png"; Bitmap bitmap = drawBitmap();
Bitmap bitmap = Bitmap.createBitmap(maxX, maxY, Bitmap.Config.RGB_565);
canvas.setBitmap(bitmap);
String mFile = path+"/drawing.png";
Bitmap bitmap = drawBitmap();
File file = new File(mFile);
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
代码是运行的,但当我打开sd卡的图像路径文件名创建,但当打开图像它是黑色的。
如何在android中从画布上捕获图像。只需传递要存储快照的surface view对象和文件路径。它工作得很好
public static void takeScreenshot(View view, String filePath) {
Bitmap bitmap = getBitmapScreenshot(view);
File imageFile = new File(filePath);
imageFile.getParentFile().mkdirs();
try {
OutputStream fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Bitmap getBitmapScreenshot(View view) {
view.measure(MeasureSpec.makeMeasureSpec(view.getWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(view.getHeight(), MeasureSpec.EXACTLY));
view.layout((int)view.getX(), (int)view.getY(), (int)view.getX() + view.getMeasuredWidth(), (int)view.getY() + view.getMeasuredHeight());
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
return bitmap;
}
什么是getBitmapScreenshotview?对不起,我发帖很匆忙,这就是为什么忘记添加第二个函数的原因。现在检查这个。希望它能解决您的问题。takeScreenshotView视图,字符串文件路径需要参数a视图,因此如何将surfaceview传递给它。surfaceview有两部分,曲面和视图。这将捕获视图,该视图通常只是用于布局的透明孔。请解释如何执行此调用。的重复?可能的重复