These two days to improve the optimization of the project image upload code, considering that there may be 7, 8M of the larger picture, because the first compression. So design to file compression, save with erase delete operation. Write down your notes here.
/** * Compress and save As, empty before saving */private void Compressfile () {//empty the old photo under Save directory string savedir = Environment.getexternalstoragedirectory () + "/bag/uploadpictures"; File Imagedir = new file (Savedir), if (Imagedir.exists ()) {Clearfolder (imagedir);} Else{imagedir.mkdirs ();} Determine the image size, greater than 300k compression for (int i = 0; i < imagepathlist.size (); i++) {Bitmap Bitmap = compressimage (Imagepathlist.get (i)); I Magepathlist.set (i, SaveImage (Savedir,bitmap));}} /** Save picture, enter save directory and bitmap, name by date, return save path * * @param path * @param bitmap * @return */private string saveimage (string path, BITM AP Bitmap) {Date dt = new Date (); SimpleDateFormat SDF = new SimpleDateFormat ("Yyyymmddhhmmsssss"); String imageName =sdf.format (DT) + ". jpg"; File File = new file (path,imagename); if (file.exists ()) {file.delete (); } try {FileOutputStream out = new FileOutputStream (file); Bitmap.compress (Bitmap.CompressFormat.JPEG, N, out); Out.flush (); Out.close (); LOG.D (TAG, "picture has been saved"); return path+ "/" +imagename; } catch (FilenotFoundexception e) {log.d (TAG, "file does not exist"); E.printstacktrace (); Return ""; } catch (IOException e) {log.d (TAG, "IO exception" +e.tostring ()); E.printstacktrace (); Return ""; }}/** * Compress picture * @param imagePath * @return */private Bitmap compressimage (String imagePath) {Photoupbitmapcache Bitmapcache = new Photoupbitmapcache ();//take 1280*720 size bitmap image = Bitmapcache.revitionimage (ImagePath, 1280,720);// Using the following line of code will cause oom, so you must use the method of Android to first compress and then import//bitmap image = Bitmapfactory.decodefile (ImagePath); Bytearrayoutputstream BAOs = new Bytearrayoutputstream () image.compress (Bitmap.CompressFormat.JPEG, +, BAOs);// Mass compression method, where 100 means no compression, the compressed data is stored in the BAOs int options = 100;while (Baos.tobytearray (). length/1024 > 300) {// The loop determines if the compressed image is greater than 100kb, which is greater than continuing to compress baos.reset ();//reset BAOs to Empty baosimage.compress (Bitmap.CompressFormat.JPEG, Options, BAOs) ;//compress options% here, store compressed data into BAOs options-= 5;//each time reduce 5%}bytearrayinputstream ISBM = new Bytearrayinputstream ( Baos.tobytearray ());//Store the compressed data BAOs into the Bytearrayinputstream bitmapBitmap = Bitmapfactory.decodestream (ISBM, NULL, NULL);//Bytearrayinputstream Data generation image log.d (TAG, "file compression succeeded"); return Bitmap;} /** * Empty the folder all sub-Files */private void Clearfolder (file file) {if (File.isdirectory ()) {file[] Chi Ldfiles = File.listfiles (); if (Childfiles = = NULL | | childfiles.length = = 0) {return; } for (int i = 0; i < childfiles.length; i++) {childfiles[i].delete (); } return; } }
Android Notes file Save, compress and erase delete