How to save view from canvas to PNG file?

独自空忆成欢 提交于 2019-12-21 04:35:18

问题


I created canvas that can be used to draw some shapes on it. How can I save its content to PNG file on user's SD card?


回答1:


check out this link this link In this link you can find the method

void saveImage() {

  try {
      String filename = Environment.getExternalStorageDirectory().toString();

      File f = new File(filename ,"myImage.png");
      f.createNewFile();
      System.out.println("file created " + f.toString());
      FileOutputStream out = new FileOutputStream(f);
      Bitmap bitmap = showImage(urlStr);
      bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
  } catch (Exception e) {
      e.printStackTrace();
  }      
 }

which is used to save the image that you got into a bitmap. and check this link for getting bitmap from canvas

hope this helps you.

Happy coding




回答2:


Canvas is just a means to draw to the Bitmap.

You should have created Canvas with new Canvas(myBitmap);. So when you draw on the Canvas, it draws to your bitmap.

so using myBitmap Do the following (code here:

String fileName = Environment.getExternalStorageDirectory() + "/test.png";
OutputStream stream = new FileOutputStream(fileName);
/* Write bitmap to file using JPEG or PNG and 80% quality hint for JPEG. */
myBitmap.compress(CompressFormat.PNG, 80, stream);
stream.close();


来源:https://stackoverflow.com/questions/13533471/how-to-save-view-from-canvas-to-png-file

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