Android save picture to SD card, use byte stream

Source: Internet
Author: User

The first few days encountered a problem: Save the image to the SD card in the Android development app, and the user searched in the gallery, similar to the form of the cache. The first idea is to change the suffix name, for example, to save a picture as Image1.txt, so save of course no problem, but in the application of reading in the not, and later did not study why not read normally, after all, this method is too earth turtle ...

Today is free to search the Internet, found that using a byte stream saved to the SD card can meet my needs. Below I put the normal save picture file code and save picture byte stream code are pasted out, convenient for everyone to learn together reference.

Suppose the name of my picture is image1.

The code that normally saves the picture file (for example, image1.png):

public static void Savephototosdcard (Bitmap photobitmap,string path,string photoname) {if (checksdcardavailable ()) { File dir = new file (path), if (!dir.exists ()) {dir.mkdirs ();} File Photofile = new file (path, Photoname + ". png"); FileOutputStream FileOutputStream = null;try {fileoutputstream = new FileOutputStream (photofile); if (photoBitmap! = null {if (photobitmap.compress (Bitmap.CompressFormat.PNG, FileOutputStream)) {Fileoutputstream.flush ();// Fileoutputstream.close ();}}} catch (FileNotFoundException e) {photofile.delete (); E.printstacktrace ();} catch (IOException e) {photofile.delete (); E.printstacktrace ();} Finally{try {fileoutputstream.close ();} catch (IOException e) {e.printstacktrace ();}}} }

Here is the code to save the picture byte stream so that the SD card will have a file named Image1.

public static byte[] Bitmaptobytes (Bitmap BM) {byte[] bytes = NULL;IF (BM! = null) {Bytearrayoutputstream BAOs = new Bytea Rrayoutputstream (); Bm.compress (Bitmap.CompressFormat.PNG, BAOs); bytes = Baos.tobytearray ();} return bytes;} public static void Savephototosdcardbyte (Bitmap photobitmap,string path,string photoname) {if (checksdcardavailable ()) {File Dir = new File (path); if (!dir.exists ()) {dir.mkdirs ();} if (Photobitmap!=null) {byte[] ByteArray = bitmaptobytes (Photobitmap); File Photofile = new file (path, photoname); FileOutputStream fileoutputstream = null; Bufferedoutputstream Bstream = null;try {fileoutputstream = new FileOutputStream (photofile); bStream = new Bufferedoutputstream (FileOutputStream); Bstream.write (ByteArray);} catch (FileNotFoundException e) {photofile.delete (); E.printstacktrace ();} catch (IOException e) {photofile.delete (); E.printstacktrace ();} Finally{try {bstream.close ();} catch (IOException e) {e.printstacktrace ();}}} (Photobitmap!=null)}}


Android save picture to SD card, use byte stream

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.