How to draw Canvas on ImageView and save Canvas as Bitmap and store in sdcard in Android

In some of the application we draw image, text and line on Canvas object in Android. Canvas is the basic component in android for drawing objects on it.

#1 Create Canvas and display it in ImageView.
Now we create a Canvas and display its content in ImageView.

  public class MyCanvas extends View {
    public MyCanvas(Context context) {
      // TODO Auto-generated constructor stub

    protected void onDraw(Canvas canvas) {
      // TODO Auto-generated method stub
      Paint pBackground = new Paint();
      canvas.drawRect(0, 0, 512, 512, pBackground);
      Paint pText = new Paint();
      canvas.drawText("Sample Text", 100, 100, pText);

Now set this canvas to your ImageView using following code.

  View v = new MyCanvas(getApplicationContext());
  Bitmap bitmap = Bitmap.createBitmap(500/*width*/, 500/*height*/, Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(bitmap);
  ImageView iv = (ImageView) findViewById(;

You can see “Sample Text” in ImageView.

#2 Save canvas bitmap to sdcard.
We will save this bitmap to sdcard so all your drawing on canvas will be stored as image.

Bitmap b = null;

//create directory if not exist
File dir = new File("/sdcard/tempfolder/");
if (!dir.exists()) {

File output = new File(dir, "tempfile.jpg");
OutputStream os = null;

try {
  os = new FileOutputStream(output);
  b.compress(Bitmap.CompressFormat.JPEG, 100, os);

  //this code will scan the image so that it will appear in your gallery when you open next time
  MediaScannerConnection.scanFile(this,	new String[] { output.toString() }, null,
    new MediaScannerConnection.OnScanCompletedListener() {
      public void onScanCompleted(String path, Uri uri) {
        Log.d("appname", "image is saved in gallery and gallery is refreshed.");
} catch (Exception e) {

If you find any problem or doubt, please mention in comments. Do not forget to share!

Download Source Code:

9 thoughts on “How to draw Canvas on ImageView and save Canvas as Bitmap and store in sdcard in Android

  1. GnanaPrakash September 11, 2015 / 1:19 pm

    Hi sir,
    i am new one for android development, i need reference for draw on image by touch, how to implement,. when i m trying canvas image are load separately.
    when i m zoom the image canvas only gets run .please give me solution to rectify this.

  2. Archa November 10, 2015 / 12:27 pm


    But using the above code ,I could save the image.But on opening,is is not having any content.

    • dakshbhatt November 18, 2015 / 5:22 am

      please check the #2 code in your project, there must be a problem which cause the result image blank.

      If you can see your content within the app, then the image should also display it if it saved properly.

      Let me know if you need any further help.

  3. wilty April 25, 2019 / 11:47 pm

    hey, is there any permssion for write sd card at manifest?

    • dakshbhatt May 9, 2019 / 4:54 am

      Yes there is 2 permission for the storage. One if for write: And second is for read:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s