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