开发者

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

开发者 https://www.devze.com 2023-03-30 03:10 出处:网络
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.

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!


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");
0

精彩评论

暂无评论...
验证码 换一张
取 消