Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7436069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:10:46+00:00 2026-05-29T10:10:46+00:00

I am making an app in which i have to draw image in canvas

  • 0

I am making an app in which i have to draw image in canvas and then SAVE that image in shared preference and then show in next screen .Any help will be appreciated

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new DrawingPanel(this));
    mPaint = new Paint();
    mPaint.setDither(true);


//    mPaint.setColor(0xFF FF FF FF);
    System.out.println("hello1");
    mPaint.setColor(Color.BLACK);
    System.out.println("hello2");

    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(3);


}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.game_menu, menu);
    return true;
}
@SuppressWarnings("null")
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.create_new: // **ON THIS CLICK I WANT TO SAVE IMAGE**

            System.out.println("hii");
        //  name = cur.getString(cur.getColumnIndex(MediaStore.Images.Media.DATA));
            System.out.println("hii1");
            date1 = currentTimeString;
            System.out.println("hii2");
        //  cur = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
            System.out.println("hii3");
        //  File f1=new File("/sdcard/" + date1 + ".png");
            System.out.println("hii4");

          //  FileSave fs = null;
        //  fs.Save(f1);
            // Add a new record without the bitmap, but with the values just set.
            // insert() returns the URI of the new record.
        //  Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);

            // Now get a handle to the file for that record, and save the data into it.
            // Here, sourceBitmap is a Bitmap object representing the file to save to the database.
            try {
                //   FileOutputStream out = new FileOutputStream(path);
                   bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
            } catch (Exception e) {
                   e.printStackTrace();
            }




            return true;

        case R.id.erase:
        System.out.println("new2");
              mPaint.setColor(-1);
             mPaint.setAlpha(0);
             mPaint.setAntiAlias(true);
             mPaint.setStyle(Paint.Style.STROKE);
             mPaint.setStrokeCap(Paint.Cap.ROUND);
             mPaint.setStrokeWidth(1);


                Intent intent1 = new Intent(getApplicationContext(), CanvasDrawingActivity.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent1);
                finish();


            return true;


        default:
            return super.onOptionsItemSelected(item);
    }
}



class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback {
    private DrawingThread _thread;
    private Path path;

    public DrawingPanel(Context context) {
        super(context);
        getHolder().addCallback(this);
        _thread = new DrawingThread(getHolder(), this);
    }




    @Override
    protected void onAttachedToWindow() {
        // TODO Auto-generated method stub
        super.onAttachedToWindow();
        System.out.println("hello");
         Save=(Button)findViewById(R.id.Save);
         System.out.println("hello2");
    }




    public boolean onTouchEvent(MotionEvent event) {
        synchronized (_thread.getSurfaceHolder()) {
        if(event.getAction() == MotionEvent.ACTION_DOWN){
        path = new Path();
        path.moveTo(event.getX(), event.getY());
        path.lineTo(event.getX(), event.getY());
        }else if(event.getAction() == MotionEvent.ACTION_MOVE){
        path.lineTo(event.getX(), event.getY());
        _graphics.add(path);
        path = new Path();
        path.moveTo(event.getX(), event.getY());
        }else if(event.getAction() == MotionEvent.ACTION_UP){
        path.lineTo(event.getX(), event.getY());
        _graphics.add(path);
        }

        return true;
        }
        }

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        for (Path path : _graphics) {
            //canvas.drawPoint(graphic.x, graphic.y, mPaint);
            canvas.drawPath(path, mPaint);
            canvas.drawPath(path, mPaint);
        }
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
        // TODO Auto-generated method stub

    }

    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        _thread.setRunning(true);
        _thread.start();
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        boolean retry = true;
        _thread.setRunning(false);
        while (retry) {
            try {
                _thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        }
    }
}

class DrawingThread extends Thread {
    private SurfaceHolder _surfaceHolder;
    private DrawingPanel _panel;
    private boolean _run = false;

    public DrawingThread(SurfaceHolder surfaceHolder, DrawingPanel panel) {
        _surfaceHolder = surfaceHolder;
        _panel = panel;
    }

    public void setRunning(boolean run) {
        _run = run;
    }

    public SurfaceHolder getSurfaceHolder() {
        return _surfaceHolder;
    }

    @Override
    public void run() {
        Canvas c;
        while (_run) {
            c = null;
            try {
                c = _surfaceHolder.lockCanvas(null);
                synchronized (_surfaceHolder) {
                    _panel.onDraw(c);
                }
            } finally {
                // do this in a finally so that if an exception is thrown
                // during the above, we don't leave the Surface in an
                // inconsistent state
                if (c != null) {
                    _surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
}

}

i want to save this image in shared preference and open in next screen with thumbnaili want to save this image in shared preference and open in next screen with thumbnail

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:10:47+00:00Added an answer on May 29, 2026 at 10:10 am

    If you have drawing cache enabled on your SurfaceView, you can get the last cached Bitmap with the getDrawingCache() method of View.

    The Bitmap object implements Parcelable; that means you can directly put it into the Intent that is launching your next Activity with Intent.putExtra(key, bitmap).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making an app in which i have a main activity that the
I am making an app in which i have to save some string in
I am making an android app in which i have to show Hash Tag
I am making an app in which i have to show Map in my
I am making an android app which will have two services that will keep
I am making an app in which i have to use image of group
I am making an app in which I have to play video file. .flv
I am making an app in which I have to retrieve the phone number
I am making an app in which I have to open default audio player.
I am making an App in which i have to play music from iPod

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.