Android notes: file storage, compression, and deletion, android notes

Source: Internet
Author: User

Android notes: file storage, compression, and deletion, android notes

In the past two days, we have improved the image upload code in the optimization project, considering that there may be large 7 or 8 m images, because we need to compress them first. Therefore, the File compression, storage, and deletion operations are designed. Take notes here.

/*** CompressFile and save it as. Each time you clear it and then save it */private void compressFile () {// clear the old photo String saveDir = Environment in the saved directory. getExternalStorageDirectory () + "/bag/uploadPictures"; File imageDir = new File (saveDir); if (imageDir. exists () {clearFolder (imageDir);} else {imageDir. mkdirs ();} // determines the image size. If the size is greater than kb, the image is compressed for (int I = 0; I <imagePathList. size (); I ++) {Bitmap bitmap = compressImage (imagePathList. get (I); imagePathList. set (I, saveImage (saveDir, bitmap) ;}/ ** Save the image, enter the Save directory and bitmap, and name it by date, return to the Save path ** @ param path * @ param bitmap * @ return */private String saveImage (String path, Bitmap 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, 100, out); out. flush (); out. close (); Log. d (TAG, "saved image"); 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 "" ;}}/*** compressed image * @ param imagePath * @ return */private Bitmap compressImage (String imagePath) {PhotoUpBitmapCache bitmapCache = new PhotoUpBitmapCache (); // obtain the Bitmap image of the size of 1280*720 = bitmapCache. revitionImage (imagePath, 1280,720); // use the following line of code to cause OOM. Therefore, you must compress the code in the Android method before importing it. // Bitmap image = BitmapFactory. decodeFile (imagePath); ByteArrayOutputStream baos = new ByteArrayOutputStream (); image. compress (Bitmap. compressFormat. JPEG, 100, baos); // quality compression method. Here, 100 indicates no compression. Store the compressed data in baos int options = 100; while (baos. toByteArray (). length/1024> 300) {// cyclically determine if the size of the compressed image is greater than kb and the size of the image is greater than that of the compressed baos. reset (); // reset baos to clear baosimage. compress (Bitmap. compressFormat. JPEG, options, baos); // compress options % here and store the compressed data in baos options-= 5; // decrease by 5% every time} ByteArrayInputStream isBm = new ByteArrayInputStream (baos. toByteArray (); // store the compressed data baos in ByteArrayInputStream Bitmap bitmap = BitmapFactory. decodeStream (isBm, null, null); // generates an image Log from ByteArrayInputStream. d (TAG, "File compressed successfully"); return bitmap;}/*** clear all the sub-files in the folder */private void clearFolder (file) {if (File. isDirectory () {File [] childFiles = file. listFiles (); if (childFiles = null | childFiles. length = 0) {return;} for (int I = 0; I <childFiles. length; I ++) {childFiles [I]. delete () ;}return ;}}



How do I clear records after a compressed file?

To prevent leaks, we can clear the records in the Start Menu. But for open compressed files, it will keep open file records in the File menu of the compression software. How should we clear the records here?

Winzip

For Winzip, we use Winzip 8.1 as an example to show you how to clear file opening records in the File menu. Open the Winzip main interface, click the "options" menu, and select the "configuration" command to switch the "configuration" window to the "Miscellaneous" tab, adjust the "number of files in the latest file list" to the minimum value of 1. Note that the names of this option may vary slightly in different Winzip versions.

WinRAR

If the compression software is WinRAR, you can open the "Settings" command under the "options" menu on the main interface, on the "General" tab, cancel the "retain compressed file history" and "allow save history in the dialog box" options, and save the settings.

The notes made on the Word compressed file clearly save the changes. What should I do if my notes are still lost next time?

If it cannot be edited and saved directly in the compressed file, it will be saved to the cache directory of drive C, but the files in the compressed file are not actually saved

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.