0

I've got an ImageView, in its onDraw(canvas),

i tried:

canvas.drawBitmap(...);//draw an extremely large background 3264 * 2448 pixels
canvas.drawLine(...);//draw target

My question is, how can I save this canvas into a sth like png? Thanks!

user1745958
  • 109
  • 5

2 Answers2

1

From the question here: Drawing on Canvas and save image

imgView.setDrawingCacheEnabled(true);
Bitmap b = imgView.getDrawingCache();

FileOutputStream fos = null;
try {
    fos = new FileOutputStream(getFileName());
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

b.compress(CompressFormat.PNG, 95, fos);
Community
  • 1
  • 1
soren.qvist
  • 7,376
  • 14
  • 62
  • 91
  • Thanks, this solution works for small bitmap drawn on ImageView, but mine is a headache of 3264 * 2448... – user1745958 Oct 27 '12 at 08:06
  • Maybe you could do it on a separate thread if you are getting ANR? – soren.qvist Oct 27 '12 at 14:17
  • Thanks but it's OOM rather than ANR. – user1745958 Oct 27 '12 at 15:40
  • Oh of course, when are you getting the error? getDrawingCache() ? – soren.qvist Oct 27 '12 at 18:08
  • 1
    Sorry but I don't know how you could solve this one, I just don't know enough about bitmaps. If you're out of memory, then you're out of memory and it's because Bitmap objects take so much memory (especially in your case). I would look into splitting up the Bitmap, I know there's a way to request more memory also (through the manifest I think), you could also look into compressing the bitmap until it fits. Maybe you should also look into if you could save the Canvas directly to disk instead of passing it to a bitmap object, I don't know if such a thing is possible. Best of luck! – soren.qvist Oct 27 '12 at 19:18
0

You can save the view cache image to the disk as png.

Techie Manoj
  • 432
  • 3
  • 14
  • do u mean sth like ImageView.setDrawingCacheEnabled(true);ImageView.getDrawingCache() – user1745958 Oct 27 '12 at 07:52
  • yes, and then get the cache image as bitmap you can save the bitmap. – Techie Manoj Oct 27 '12 at 07:56
  • this works for small background bitmap, my headache is that my bitmap is extremely large @ 3264 * 2448 pixels. I am able to display it on an ImageView, yet fail to getDrawingCache()... I've also tried sth like measure(...);layout(...); still crashed... – user1745958 Oct 27 '12 at 08:02