Saving canvas to bitmap on Android

前端 未结 5 1497
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-05 13:24

I\'m having some difficulty with regards to placing the contents of a Canvas into a Bitmap. When I attempt to do this, the file gets written with a file size of around 5.80K

5条回答
  •  借酒劲吻你
    2021-01-05 13:58

    I had similar problem and i've got solution. Here full code of a task /don't forget about android.permission.WRITE_EXTERNAL_STORAGE permission in manifest/

      public Bitmap saveSignature(){
    
          Bitmap  bitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.ARGB_8888);
          Canvas canvas = new Canvas(bitmap);
          this.draw(canvas); 
    
          File file = new File(Environment.getExternalStorageDirectory() + "/sign.png");
    
          try {
               bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(file));
          } catch (Exception e) {
               e.printStackTrace();
          }
    
          return bitmap;
      }
    

提交回复
热议问题