Dinusha
Dinusha

Reputation: 11

how to load a PNG image on sdcard to a Canvas to draw in Android?

My app is a basic drawing app. User can draw on a Canvas and save the image as a PNG. He can load the earlier drawn images and edit them.

I was able to do the first part. that is, the user can draw and save the image on the sdcard. I'm having trouble loading the saved png file on to the Canvas and drawing on it.

here is the run method in my SurfaceView class.

public void run() {
            Canvas canvas = null;
            while (running) {
                try {
                    canvas = holder.lockCanvas(null);
                    synchronized (holder) {
                        if(mBitmap == null){
                            mBitmap =  Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);;
                        }
                        final Canvas c = new Canvas (mBitmap);
                        c.drawColor(Color.WHITE);

                        //pad.onDraw(canvas);

                        Paint p = new Paint();
                        p.setColor(Color.GRAY);

                        for(double x = 0.5;x < c.getWidth();x += 30) {
                            c.drawLine((float)x, 0, (float)x, c.getHeight(), p);
                        }

                        for(double y= 0.5;y < c.getHeight();y += 30) {
                            c.drawLine(0, (float)y, c.getWidth(), (float)y, p);
                        }

                        pad.onDraw(c);

                        canvas.drawBitmap (mBitmap, 0,  0, null);
                    }
                } finally {
                    if (canvas != null) {
                        holder.unlockCanvasAndPost(canvas);
                    }
                }
            }
        }

I tried loading the png to the 'mBitmap', but it didn't work. Any help appreciated.

thank you!

Upvotes: 1

Views: 3567

Answers (1)

Caner
Caner

Reputation: 59168

In your code you are not loading the image from sd card at all, is this intentional? This is how you open an image form SD card:

mBitmap = BitmapFactory.decodeFile("/sdcard/test.png");

Upvotes: 4

Related Questions