android:将canvas转换为位图,然后保存到SD卡

时间:2012-01-22 15:58:06

标签: android android-canvas

我正在开发一个Android应用程序,我可以在画布上绘制。我想将我的画布转换为位图,然后将其以jpeg格式保存在我的SD卡上。

我该怎么做呢?

2 个答案:

答案 0 :(得分:5)

这样的事情应该有效:

http://developer.android.com/reference/android/view/View.html#getDrawingCache(boolean)

public void toJPEGFile(){
    File folder = new File(Environment.getExternalStorageDirectory()+"/folder/");
    if(!folder.exists()) folder.mkdirs();

    try {
        this.setDrawingCacheEnabled(true);
        FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory()+"/folder/file"));
        Bitmap bitmap = this.getDrawingCache();
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}

答案 1 :(得分:2)

你查了一下文件吗?

Canvas API

您可以使用:

public Canvas (Bitmap bitmap)

    Since: API Level 1
    Construct a canvas with the specified bitmap to draw into. The bitmap must be mutable.
    The initial target density of the canvas is the same as the given bitmap's density.
    Parameters

    bitmap  Specifies a mutable bitmap for the canvas to draw into.