Android-save several formats as jpeg files

Source: Internet
Author: User

Android-save several formats as jpeg files

// 1. the YuvImage class in NV21 format is used for storage efficiency. String fileName = "IMG _" + String. valueOf (index) + ". jpg "; File sdRoot = Environment. getExternalStorageDirectory (); String dir = "/picture/"; File mkDir = new File (sdRoot, dir); if (! MkDir. exists () {mkDir. mkdirs ();} File pictureFile = new File (sdRoot, dir + fileName); if (! PictureFile. exists () {try {pictureFile. createNewFile (); FileOutputStream filecon = new FileOutputStream (pictureFile); YuvImage image = new YuvImage (data, ImageFormat. NV21, size. width, size. height, null); image. compressToJpeg (new Rect (0, 0, image. getWidth (), image. getHeight (), 70, filecon); // compress an NV21 image with a quality of 70 to Jpeg and obtain the JPEG data stream} catch (IOException e) {e. printStackTrace () ;}// 2. bitmap bmp = BitmapFactory is used. decodeFile (pictureFile. getPath (); FileOutputStream fileOutStream = null; fileOutStream = new FileOutputStream (file); bmp. compress (Bitmap. compressFormat. JPEG, 100, fileOutStream); // outputs the bitmap to the specified file. flush (); fileOutStream. close (); // 3. save byte [] data FileOutputStream fos = new FileOutputStream (pictureFile. getPath (); // Get file output streamfos. write (bytes); // Written to the filefos. close ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.