android: convert canvas to bitmap then save to SD card

后端 未结 2 510
北荒
北荒 2021-01-25 18:32

i\'m developing an android app where I can draw on a canvas. I want to convert my canvas to bitmap and then save it in jpeg format on my sd card..

how can i properly do

2条回答
  •  [愿得一人]
    2021-01-25 19:03

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

提交回复
热议问题