KcYxA
KcYxA

Reputation: 243

Saving a Drawing

I am building an app that uses Android's Canvas and Drawables.

What is the best way to save a drawing for later modification? Say it has lines and icons that I want a user to be able to save and still be able to modify each component of the drawing at a later date.

I know I can save the drawing as a .png or .jpg, but then it can't be edited later. I found a way to render SVG files to Canvas, but not the other way around.

Upvotes: 1

Views: 378

Answers (3)

Shreyash Mahajan
Shreyash Mahajan

Reputation: 23596

If you want to save it which can also be able to editabel after saving it and reopen it then we will have to check for it.

But if you want to just save the drawing your drawing and the canvas then try this:

@Override
                public void onClick(View v) {
                     final Activity currentActivity  = YourActivity.this;
                    Handler saveHandler = new Handler(){
                        @Override
                        public void handleMessage(Message msg) {
                            final AlertDialog alertDialog = new AlertDialog.Builder(currentActivity).create();
                            alertDialog.setTitle("Your Title");
                            alertDialog.setMessage("Your drawing is saved to Gallery.");
                            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    return;
                                }
                            });
                            alertDialog.show();
                        }
                    } ;
                   new ExportBitmapToFileToSaveInGallery(TWSBIDrawMainActivity.this,saveHandler, bm).execute();







 // to Saw progressBar and also the dialog box that gives confirmation to user.
public static class ExportBitmapToFileToSaveInGallery extends AsyncTask<Intent,Void,Boolean> {
    private Context mContext;
    private Handler mHandler;
    private Bitmap nBitmap;
    private ProgressDialog  m_progressDialog = null; 
    @Override     
    protected void onPreExecute(){         
        m_progressDialog = new ProgressDialog(mContext);  
        m_progressDialog.setTitle("Draw");
        m_progressDialog.setMessage("Please wait...");
        m_progressDialog.setCancelable(false);         
        m_progressDialog.show();     
    }

    public ExportBitmapToFileToSaveInGallery(Context context,Handler handler,Bitmap bitmap) {
        mContext = context;
        nBitmap = bitmap;
        mHandler = handler;
    }

    @Override
    protected Boolean doInBackground(Intent... arg0) {
        try {
            if (!APP_FILE_PATH.exists()) {
                APP_FILE_PATH.mkdirs();
            }

            final FileOutputStream out = new FileOutputStream(new File(APP_FILE_PATH + "/"+timeStampFormat.format(new Date())+".jpg"));
            nBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);

            out.flush();
            out.close();

            return true;
        }catch (Exception e) {
            e.printStackTrace();
        }
        //mHandler.post(completeRunnable);
        return false;
    }

    @Override
    protected void onPostExecute(Boolean bool) {
        super.onPostExecute(bool);
        if ( bool ){
            mHandler.sendEmptyMessage(1);
        }
        if (m_progressDialog.isShowing()) {             
            m_progressDialog.dismiss();          
        }  

    }
}

This will store your canvas drawing to the formate you want and once it saving you can also add some drawing as you want. Hope it will help you little.

Thanks.

Upvotes: 1

yingted
yingted

Reputation: 10544

You need to save the list of primitives you draw, since the canvas just uses Bitmap. In addition, you have to store the z-index, which determines what order they appear in, in order to allow the user to reorder elements.

It might actually be easier to define a new EditableShape class, which holds the controls (i.e. endpoints for a line, center and two axes for an ellipse), which can be written to file.

For the images, you probably have to store a copy of the image with a unique id attached instead of just the address in case the original is deleted.

Upvotes: 1

Nick Badal
Nick Badal

Reputation: 671

You would have to store each action you call on the canvas, somehow, as there's no way in my knowledge to retrieve them once they've been applied to the canvas.

Perhaps by extending the Canvas class, and overriding each function to store the action in a file or database, then running the superclass's method?

Upvotes: 0

Related Questions