ADBlock is blocking some content on the site

ADBlock errore
Results found: 


I am new to canvas. I want to use the My already saved Image and want some paint on that image. after that i want to save it.

I know that with using Canvas it is possible. I can able to do painting on the Image but while i am going to store that image it only saved the painting. Not the Image with painting.

So can anybudy tell me code of how to paint on image and save that image ?


Here is my Code that use to do paint on the SurfaceView. Source Code:

        public void run() {
            //Canvas canvas = null;
            while (_run){

                    canvas = mSurfaceHolder.lockCanvas(null);
                    if(mBitmap == null){
                        mBitmap =  Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);

                    final Canvas c = new Canvas (mBitmap);
                    //canvas.drawColor(0, PorterDuff.Mode.CLEAR);
                    c.drawColor(0, PorterDuff.Mode.CLEAR);
//                  Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.icon);

//                    if(!(DrawingActivity.imagePath==null)){
//                      canvas.drawBitmap(DrawingActivity.mBitmap, 0, 0, null);
//                    }
                    canvas.drawBitmap (mBitmap, 0,  0,null);
                } finally {



I am using mBitmap to save the Bitmap to the SDCard.


Your problem is your drawing over and over on your entire canvas:

 final Canvas c = new Canvas (mBitmap); // creates a new canvas with your image is painted background
 c.drawColor(0, PorterDuff.Mode.CLEAR); // this makes your whole Canvas transparent
 canvas.drawColor(Color.WHITE);  // this makes it all white on another canvas
 canvas.drawBitmap (mBitmap, 0,  0,null); // this draws your bitmap on another canvas

Use logic roughly like this:

public void run() {

Canvas c = new Canvas(mBitmap);

/* Paint your things here, example: c.drawLine()... Beware c.drawColor will fill your canvas, so your bitmap will be cleared!!!*/

/* Now mBitmap will have both the original image & your painting */
String path = Environment.getExternalStorageDirectory().toString(); // this is the sd card
OutputStream fOut = null;
File file = new File(path, "MyImage.jpg");
fOut = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);

Also don't forget to add necessary permission to save your file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

outside <application></application> in your manifest file.

If you like, leave us your opinion

Was the article helpful and is it translated correctly?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow