Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中保存绘图画布?_Android_Android Layout_Android Emulator_Android Widget_Android Canvas - Fatal编程技术网

如何在android中保存绘图画布?

如何在android中保存绘图画布?,android,android-layout,android-emulator,android-widget,android-canvas,Android,Android Layout,Android Emulator,Android Widget,Android Canvas,我正在使用开发者网站的API演示 但我想知道如何将该图像保存到我的Andrtoid设备中。 请任何人给出将绘制的图像保存到Android设备的代码 谢谢。一个选项是创建另一个画布(如下所示),并在此新画布上重复所有绘图。 完成后,调用drawBitmap Bitmap bitmap = new Bitmap(// Set the params you like //); Canvas canvas = new Canvas(bitmap); // Do all your drawings he

我正在使用开发者网站的API演示

但我想知道如何将该图像保存到我的Andrtoid设备中。 请任何人给出将绘制的图像保存到Android设备的代码


谢谢。

一个选项是创建另一个画布(如下所示),并在此新画布上重复所有绘图。 完成后,调用drawBitmap

Bitmap bitmap = new Bitmap(// Set the params you like //);
Canvas canvas = new Canvas(bitmap);

// Do all your drawings here

canvas.drawBitmap(// The first picture //);
最好的方法是,如果有一种方法可以复制现有的画布,然后您不需要重新绘制所有内容,但我找不到任何内容。

试试这段代码

View content = your_view;
content.setDrawingCacheEnabled(true);
content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap bitmap = content.getDrawingCache();
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
File file = new File(path+"/image.png");
FileOutputStream ostream;
try {
    file.createNewFile();
    ostream = new FileOutputStream(file);
    bitmap.compress(CompressFormat.PNG, 100, ostream);
    ostream.flush();
    ostream.close();
    Toast.makeText(getApplicationContext(), "image saved", 5000).show();
} catch (Exception e) {
    e.printStackTrace();
    Toast.makeText(getApplicationContext(), "error", 5000).show();
}

然后使用位图工厂将位图写入文件。

我已经实现了以下方法并为我工作。 通过使用xml文件中的id获取CustomView,而不是通过实例化CustomView

View v=findviewbyd(R.id.custom\u视图);
//不要通过这种方式获取customview,视图v=新的customview(此);
int canvasWidth=v.getWidth();
int canvasHeight=v.getHeight();
位图Bitmap=Bitmap.createBitmap(canvasWidth、canvasHeight、Bitmap.Config.ARGB_8888);
画布=新画布(位图);
v、 绘画(画布);
ImageView ImageView=findViewById(R.id.image\u视图);

设置图像位图(位图)访问以下[链接][1]获取答案。[1] :如何保存
视图的背景?现在只保存图形而不保存背景;如果我错了,纠正我,我应该把我的布局id或视图id?我做了这个View content=findviewbyd(R.id.content);我在一个空对象引用上得到了“试图调用虚拟方法'void android.view.view.setDrawingCacheEnabled(布尔)'”
drawView.setDrawingCacheEnabled(true);
Bitmap bm = null;
drawView.destroyDrawingCache();
bm=drawView.getDrawingCache();