为什么我无法将表面视图上的画布绘图保存到Android中的SD卡

时间:2012-09-17 06:13:44

标签: android-canvas

在我的Paint应用程序中,我需要保存绘制的图像。我的代码正在运行,但图片没有保存到SD卡。我使用的代码如下。

drawingSurfaceView.buildDrawingCache();
    Bitmap bmap = drawingSurfaceView.getDrawingCache();
    String extStorageDirectory = Environment.getExternalStorageDirectory()
            .toString();
    File file = new File(extStorageDirectory, "imag.PNG");
    FileOutputStream outStream;
    try {
        outStream = new FileOutputStream(file);
        bmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);

        MediaStore.Images.Media.insertImage(getContentResolver(),
                file.getAbsolutePath(), file.getName(), file.getName());

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

请告诉我该代码有什么问题

1 个答案:

答案 0 :(得分:0)

我已经通过使用getters和bitmap的setter解决了,比如drwaSurfaceView.setBitmap() 和drwaSurfaceView.getBitmap()。在我的surfaceview类中,我创建了`public Bitmap getBitmap(){         返回缩放;     }

public void setBitmap(Bitmap scaled) {
    this.scaled = scaled;
}`

并且在Thread类中我将保存绘制和背景图像,如`class DrawThread extends Thread {         私人SurfaceHolder mSurfaceHolder;

    public DrawThread(SurfaceHolder surfaceHolder) {
        mSurfaceHolder = surfaceHolder;

    }

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

    @Override
    public void run() {
        Canvas canvas = null;
        while (_run) {
            if (isDrawing == true) {
                try {
                    canvas = mSurfaceHolder.lockCanvas(null);

                    if (scaled == null) {
                        scaled = Bitmap.createScaledBitmap(background, w,
                                h, true);
                    }

                    if (DrawAppActivity.saveBtnFlag == 1) {
                        DrawAppActivity.saveBtnFlag = 0;
                        final Canvas c = new Canvas(scaled);
                        commandManager.executeAll(c);
                        canvas.drawBitmap(scaled, 0, 0, null);
                        commandManager.executeAll(canvas);
                    } else {
                        canvas.drawBitmap(scaled, 0, 0, fillPaint);
                        commandManager.executeAll(canvas);
                    }

                } finally {
                    mSurfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        }
    }
}`