1

I am making a drawing app in android.

Here I am saving the drawing on canvas as png image. I am using Bitmap array to save the drawing from canvas.

if(view.getId()==R.id.save_btn){
    //save drawing
    AlertDialog.Builder saveDialog = new AlertDialog.Builder(this);
    saveDialog.setTitle("Save drawing");
    saveDialog.setMessage("Save drawing to device Gallery?");
    saveDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int which){
            int k=0;
            //save drawing

            while(bits[k]!=null) {
                Bitmap m = null;

                m = bits[k];
                String path = Environment.getExternalStorageDirectory().getAbsolutePath();
                File file = new File(path + File.separator + "Pictures" + File.separator + k + "_image.png");
                FileOutputStream ostream;
                try {
                    file.createNewFile();
                    ostream = new FileOutputStream(file);
                    m.compress(Bitmap.CompressFormat.PNG, 100, ostream);
                    ostream.flush();
                    ostream.close();
                    Toast.makeText(getApplicationContext(), "image saved :" + path, Toast.LENGTH_SHORT).show();


                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
                }
                k=k+1;
            }

            i=0;
            bits=null;
        }
    }
}

Here I am saving the drawingCache of the view in to array of bitmap. Here even if my canvas is empty still an blank image is saved.

I dont want that. I want to save only if there is any drawing on the canvas.
So is there any way to do this??

Or is there any way to check whether there is any drawing on canvas??

Pozzo Apps
  • 1,859
  • 2
  • 22
  • 32
JCoder
  • 113
  • 1
  • 2
  • 9
  • Are you able to set a flag/boolean that gets changed after the first drawing is performed? – petey Oct 16 '15 at 15:52
  • I didnt try setting any flag. I went pretty much with Henry 's answer. But I think that is possible. – JCoder Oct 17 '15 at 06:24

2 Answers2

1

Don't use the drawingCache. Have a method in your View like getBitmap() and use that method to get the bitmap. You are using the default bitmap of the canvas. This is hard to manipulate, and instead create your own bitmap and then draw on top of that bitmap. This is a far better way, not only for your requirement but for manipulating the bitmap too.

UPDATE: You can create your own bitmap and draw on it as follows

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);

Now you can use this canvas object to draw what you want. In my application, I have dedicated a whole class to take care of the drawing. I use the onDraw() of the CustomView to just view this bitmap and to zoom. The original drawing happens in my dedicated class. I have overridden the onTouchEvent() and used that to handle various drawing gestures and use these inputs to draw on my custom bitmap.

But if you are going to use the drawingCache, then check this one out : How to check if a Bitmap is empty (blank) on Android

Community
  • 1
  • 1
Henry
  • 17,490
  • 7
  • 63
  • 98
  • Hey, how can we create our own bitmap and use it?? – JCoder Oct 16 '15 at 14:59
  • Hey, I have updated my answer above. Hope this helps you. I too initially used the default bitmap of the CustomView to derive my final resulting bitmap. But later when things like zooming and scaling came in, it soon became a nightmare. So I just separated the actual drawing and the viewing of the drawn bitmap. – Henry Oct 16 '15 at 15:54
  • Thanks @Henry... I did as u told... I made a custom bitmap... Now I have to create a method for getting the bitmap right? So is this return statement correct for the method **return this.getDrawingCache()** ??? – JCoder Oct 16 '15 at 17:03
  • this.getDrawingCache() will return the default bitmap of the view. You never need to do this. Instead create a method like drawBitmap() and inside it create a bitmap and assign a canvas for it. Perform all canvas.draw() methods on this, and finally return the bitmap. To show this bitmap in the CustomView, in the onDraw() method, just call canvas.drawBitmap() and pass in this bitmap. – Henry Oct 16 '15 at 17:35
  • Ok got it.. Thanks again dude..:):) – JCoder Oct 16 '15 at 18:55
0

please see the code at https://stackoverflow.com/a/29938454/755804 , it checks if a given Unicode character is drawn or shown as whitespace.

The check is done in:

boolean res = !orig.sameAs(bitmap);
Community
  • 1
  • 1
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127