0

在我的 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();
    }

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

4

1 回答 1

0

我已经通过使用 getters 和位图设置器(如 drwaSurfaceView.setBitmap() 和 drwaSurfaceView.getBitmap())解决了问题。在我的表面视图类中,我创建了 `public Bitmap getBitmap() { return scaled; }

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

在 Thread 类中,我将保存绘画和背景图像,例如 `class DrawThread extends Thread { private 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);
                }
            }
        }
    }
}`
于 2012-10-05T07:31:28.340 回答