
// MainActivity.java section
// =================

// save file in MainActivity upon button hit

        // we load the same picture in smaller format
        String path = "/storage/sdcard0/Download/";
        String fileName = "drawing.jpg";
        Bitmap bitmapOrg = BitmapFactory.decodeFile(path+fileName);

        Bitmap finalBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 208, 442, null, true);
        BitmapDrawable bitmapDrawable = new BitmapDrawable(finalBitmap);
        //Canvas canvas = new Canvas(finalBitmap);
        D.canvas.drawBitmap(bitmapDrawable.getBitmap(), 0, 0, paint);

        // example modifying smaller picture
        Paint paint = new Paint();
        D.canvas.drawLine(50, 50, 150, 150, paint);
       
        // save smaller picture with additional drawings on it
        File file = new File(path+"/bitmaporg-copy.jpg");
        FileOutputStream ostream;
        try {
            file.createNewFile();
            ostream = new FileOutputStream(file);
            // saves picture but no drawings on it...
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
            ostream.flush();
            ostream.close();
            Toast.makeText(mContext, "image saved", 5000).show();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(mContext, "error", 5000).show();
        }

--------------------

// Main.xml extract
// ================

       <ree.painter.PaintSurface
           android:id="@+id/surfaceView"
           android:layout_width="416dip"
           android:layout_height="884dip"
           android:visibility="visible"
           android:layout_gravity="center_vertical|center_horizontal"/> 

--------------------

// PaintSurface.java
// =================
// drawing works perfectly using this setup

public class PaintSurface extends SurfaceView implements SurfaceHolder.Callback, Runnable, OnTouchListener {

    // Surface holder allows to control and monitor the surface
    private SurfaceHolder mHolder;

    // A thread where the painting activities are taking place
    private Thread mThread;

    // A flag which controls the start and stop of the repainting of the SurfaceView
    private boolean mFlag = false;

    private static Paint mPaint;

    private static Canvas canvas;
   
    // these added but not used
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    }
   
    public void surfaceCreated(SurfaceHolder holder) {
    }
   
    public void surfaceDestroyed(SurfaceHolder holder) {
    }
   
    public PaintSurface(Context context, AttributeSet attrs) {
        super(context, attrs);
    
        // initialise
        D.surface = this;
           
        // Getting the holder
        mHolder = getHolder();
    
        // Initializing the paint object mPaint
        mPaint = new Paint();
    
        // Setting the color for the paint object
        mPaint.setColor(Color.BLUE);    
           
        // allow use of onDraw
        setWillNotDraw(false);
    }

    public void resume(){
        // Instantiating the thread
        mThread = new Thread(this);

        // setting the mFlag to true for start repainting
        mFlag = true;

        // Start repaint the SurfaceView
        mThread.start();
    }

    public void pause(){
        mFlag = false;
    }

    static D data = null;
    static int mx = 0, my = 0;
    static int oldx = 0, oldy = 0;
   
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // here handling drawing commands stored in polygons
        ....
    }

...........

    public void run() {
        while(mFlag){
           
            try { Thread.sleep(10); } catch (Exception e) { }
           
            // Check whether the object holds a valid surface
            if(!mHolder.getSurface().isValid()) continue;
               
            // Start editing the surface
            canvas = mHolder.lockCanvas();
           
            if (data == null) {
                data = new D();
                data.canvas = canvas;
                data.screenW = canvas.getWidth();
                data.screenH = canvas.getHeight();
               
            }   

            D.canvas.drawLine(50, 50, 150, 150, D.paintG);
            drawFrame();
            paintImage();
            drawAllPolygons();
            fillAllPolygons();
           
           
            // Finish editing the canvas and show to the user
            mHolder.unlockCanvasAndPost(D.canvas);   
           
        }
    }
   
    static boolean onDraw(View v) {
        // the following never happens
        Log.i("SurfaceView", "onDraw being called...");
        return true;
    }
