How to save drawing canvas in android?

I am using this demo of the developer site API, THIS IS A DEMO.

But I am wondering how to save this image in My Andrtoid Device. Is someone please give Code to save this inverse image on an Android device.

Thanks.

+4
source share
3 answers

try this code

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(); } 
+12
source
 drawView.setDrawingCacheEnabled(true); Bitmap bm = null; drawView.destroyDrawingCache(); bm=drawView.getDrawingCache(); 

Then write the bitmap to the file using the factory bitmap.

+1
source

One option is to create another canvas (as shown below) and repeat the entire drawing on this new canvas. After that call 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 //); 

It would be best if there was a way to copy an existing canvas, and then you would not need to re-draw everything, but I could not find it.

0
source

Source: https://habr.com/ru/post/1381850/


All Articles