android: convert canvas to bitmap then save to SD card

心已入冬 提交于 2019-12-02 03:59:35

Something like that should work :

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



}

Did you look up the documentation?

Canvas API

You can use the:

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.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!