android image draws of five canvases save the image in the specified format and size

  • 2020-05-09 19:16:29
  • OfStack

Edit the image (shrink, doodle, etc.) and save it to the specified format and size.
First paste the code:
 
Bitmap bmp = Bitmap.createBitmap(480, 800, Config.ARGB_8888); 
Canvas canvas = new Canvas(bmp); 
canvas.drawBitmap(this.bmp, matrix, paint); 
canvas.save(Canvas.ALL_SAVE_FLAG); 
canvas.restore(); 

File file = new File("/sdcard/akai/"); 
if(!file.exists()) 
file.mkdirs(); 
try { 
FileOutputStream fos = new FileOutputStream(file.getPath() + "/2.png"); 
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos); 
fos.close(); 
System.out.println("saveBmp is here"); 
} catch (Exception e) { 
e.printStackTrace(); 
} 

Explanation:
1. First create an Bitmap image and specify the size;
2. Create a new canvas Canvas on the image, then draw it on the canvas and save it;
3, need to save the directory File, note that if the directory such as "/sdcard/akai/" does not exist, to create (file.mkdirs ()), otherwise FileOutputStream will report an error No found;
4. Permissions need to be added: < uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/ >

Related articles: