0

我正在尝试将画布保存为图像格式....我正在使用 SurfaceView 类在画布上绘制并使用以下代码创建画布:

Bitmap saveBitmap = Bitmap.createBitmap(getWindowManager()
                    .getDefaultDisplay().getWidth(), getWindowManager()
                    .getDefaultDisplay().getHeight(),
                    Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(saveBitmap);
c = holder.lockCanvas();
onDraw(c);
holder.unlockCanvasAndPost(c);

之后我的保存图像功能如下:

public void saveImage() {

    AlertDialog.Builder editalert = new AlertDialog.Builder(
            PaintActivity.this);
    editalert.setTitle("Please Enter the name with which you want to Save");
    final EditText input = new EditText(PaintActivity.this);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.FILL_PARENT);
    input.setLayoutParams(lp);
    editalert.setView(input);
    editalert.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // dp.setDrawingCacheEnabled(true);
                    String name = input.getText().toString();
                    // Bitmap bitmap = dp.getDrawingCache();
                    File path = new File(Environment
                            .getExternalStorageDirectory()
                            + File.separator
                            + "Image Effect");
                    if (!path.exists()) {
                        path.mkdir();
                    }
                    File file = new File(path, name + ".jpg");
                    if (file.exists())
                        file.delete();
                    try {
                        if (!file.exists()) {
                            file.createNewFile();
                        }
                        FileOutputStream ostream = new FileOutputStream(
                                file);
                        // bitmap.compress(CompressFormat.JPEG, 50,
                        // ostream);
                        saveBitmap.compress(Bitmap.CompressFormat.JPEG,
                                100, ostream);
                        ostream.flush();
                        ostream.close();
                        dp.invalidate();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        Toast.makeText(PaintActivity.this,
                                "Succesfully saved", Toast.LENGTH_LONG)
                                .show();
                        // dp.setDrawingCacheEnabled(false);
                    }
                }
            });

     editalert.show();
}

任何帮助,将不胜感激。

4

0 回答 0