How to Save the drawing canvas in android?
Asked Answered
N

4

7

I am using this API demo of the Developer site, THIS DEMO.

But i am wonder that how to save that image in to My Andrtoid Device. Is please anyone give the Code to save that drawn image to the Android Device.

Thanks.

Northwestward answered 18/11, 2011 at 7:24 Comment(1)
Visit Following [Link][1] for the Answer. [1]: https://mcmap.net/q/508099/-image-on-canvas-to-jpeg-fileSeligman
C
13

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();
}
Commensal answered 18/11, 2011 at 7:33 Comment(2)
How can I also save the Background of the view? Right now that's saving the drawing only but not the background.Varrian
The answer said View content = you_view; Correct me If I'm wrong, am I supposed to put my layout id or view id? I did this View content = findViewById(R.id.content); and I got " Attempt to invoke virtual method 'void android.view.View.setDrawingCacheEnabled(boolean)' on a null object reference"Douche
P
1
drawView.setDrawingCacheEnabled(true);
Bitmap bm = null;
drawView.destroyDrawingCache();
bm=drawView.getDrawingCache();

Then write the bitmap to file using bitmap factory.

Photofinishing answered 18/11, 2011 at 7:35 Comment(0)
T
0

One option is create another Canvas (as shown below) and repeat all your drawing on this new canvas. Once done, 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 //);

The best would be if there was a way to copy an existing canvas and then you wont need to re-draw everything but I couldn't find one.

Takara answered 18/11, 2011 at 7:32 Comment(0)
S
0

I have implemented the below approach & worked for me. Get your CustomView by using its id from xml file but not by instantiating the Customview.

View v = findViewById(R.id.custom_view);
//don't get customview by this way, View v = new CustomView(this);
int canvasWidth = v.getWidth();
int canvasHeight = v.getHeight();
Bitmap bitmap = Bitmap.createBitmap(canvasWidth, canvasHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
ImageView imageView = findViewById(R.id.image_view);
imageView.setImageBitmap(bitmap);

All code should be inside saveButton click listener.

Scevor answered 16/5, 2020 at 20:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.