Android App - How to save a bitmap drawing on canvas as image? Check code?

前端 未结 2 1456
攒了一身酷
攒了一身酷 2021-02-04 12:46

I tried to use the following codes to

  1. Draw on canvas
  2. Save the canvas on Image

Problem - When I try to save the image, it shows a null

2条回答
  •  迷失自我
    2021-02-04 13:00

    There is a little add on in the above code. The above code is saving the pic in the storage. But the Image is not showing in the gallery. To show the image in gallery just need to setup a MediaScannerConnection for the bitmap we are saving

    Sample Function

    public void scanPhoto(final String imageFileName) {
        MediaScannerConnection msConn = new MediaScannerConnection(PaintPic.this,
                new MediaScannerConnectionClient() {
                    public void onMediaScannerConnected() {
                        msConn.scanFile(imageFileName, null);
                        Log.i("msClient obj  in Photo Utility",
                                "connection established");
                    }
    
                    public void onScanCompleted(String path, Uri uri) {
                        msConn.disconnect();
                        Log.i("msClient obj in Photo Utility", "scan completed");
                    }
                });
        msConn.connect();
    }
    

    and call this function just after this line

    save.compress(Bitmap.CompressFormat.PNG, 100, ostream);
    

    Now the saved bitmap is also visible in the Gallery

提交回复
热议问题