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.

stacker
  • 68,052
  • 28
  • 140
  • 210
Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
  • 1
    Visit Following [Link][1] for the Answer. [1]: http://stackoverflow.com/questions/2174875/android-canvas-to-jpg – Lucifer Nov 18 '11 at 07:33

4 Answers4

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();
}
100rabh
  • 6,156
  • 5
  • 27
  • 41
Pratik
  • 30,639
  • 18
  • 84
  • 159
  • 1
    How can I also save the Background of the `view`? Right now that's saving the drawing only but not the background. – Si8 Aug 30 '13 at 15:02
  • 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" – makkhay gurung May 11 '17 at 03:05
1
drawView.setDrawingCacheEnabled(true);
Bitmap bm = null;
drawView.destroyDrawingCache();
bm=drawView.getDrawingCache();

Then write the bitmap to file using bitmap factory.

Jana
  • 2,890
  • 5
  • 35
  • 45
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.

Gopal Dan
  • 304
  • 2
  • 5
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.

Lior Ohana
  • 3,467
  • 4
  • 34
  • 49