将画布保存到jpg图像只给黑色图像安卓

时间:2013-09-26 07:15:45

标签: android image save android-canvas

我正在尝试将画布保存为图像格式....我使用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();
}

任何帮助都将不胜感激。

0 个答案:

没有答案