Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save drawing to disk using Parcelable

I am using this code to save and restore my custom hand drawing view, how can I use this code to save my drawings in vector format, not bitmap, to a file on sdcard and reload it in next app session:

@Override
protected Parcelable onSaveInstanceState() {

    // Get the superclass parcelable state
    Parcelable superState = super.onSaveInstanceState();

    if (mPoints.size() > 0) {// Currently doing a line, save it's current path
        createHistoryPathFromPoints();
    }

    return new FreeDrawSavedState(superState, mPaths, mCanceledPaths,
            mCurrentPaint, mPaintColor, mPaintAlpha, mResizeBehaviour,
            mLastDimensionW, mLastDimensionH);
}

@Override
protected void onRestoreInstanceState(Parcelable state) {

    // If not instance of my state, let the superclass handle it
    if (!(state instanceof FreeDrawSavedState)) {
        super.onRestoreInstanceState(state);
        return;
    }

    FreeDrawSavedState savedState = (FreeDrawSavedState) state;
    // Superclass restore state
    super.onRestoreInstanceState(savedState.getSuperState());

    // My state restore
    mPaths = savedState.getPaths();
    mCanceledPaths = savedState.getCanceledPaths();
    mCurrentPaint = savedState.getCurrentPaint();

    initFillPaint();

    mResizeBehaviour = savedState.getResizeBehaviour();

    mPaintColor = savedState.getPaintColor();
    mPaintAlpha = savedState.getPaintAlpha();
    // Restore the last dimensions, so that in onSizeChanged i can calculate the
    // height and width change factor and multiply every point x or y to it, so that if the
    // View is resized, it adapt automatically it's points to the new width/height
    mLastDimensionW = savedState.getLastDimensionW();
    mLastDimensionH = savedState.getLastDimensionH();

    notifyRedoUndoCountChanged();
}
like image 461
AVEbrahimi Avatar asked Feb 06 '23 03:02

AVEbrahimi


1 Answers

You need to create your own file format for storing your custom vector drawing.

Saving all your points and their properties into a JSON files is a possible solution.

Android's Parcelable is not meant for persistent storage, just for in-app or inter process communication. Because the binary format can change between Android updates for whatever reasons e.g. smaller memory or cpu footprint of the new binary format. Thus loading after such update will lead to loss of data.

like image 171
shelll Avatar answered Feb 15 '23 11:02

shelll