Local Cache
Asynchronous download
As we all know, in android applications, if the UI thread does not respond within 5 seconds, it will throw a non-response exception. for remote access to large resources, this exception will easily be thrown out, so how can we avoid this problem. There are two ways to do this in android:
Start a new thread to obtain resources. After the completion, send messages through the Handler mechanism and process messages in the UI thread to obtain images in the asynchronous thread, then, the process of updating the UI thread through Handler Message.
Use AsyncTask provided in android.
The specific method is not described here. You can check the API, or google or baidu. The local cache is used here.
Local Cache
For image resources, you cannot allow applications to download images remotely every time they obtain them. This wastes resources, however, you cannot place all image resources in the memory (although loading will be faster), because image resources usually occupy a large amount of memory space, which may easily lead to OOM. If the downloaded image is saved to the SDCard, will it be directly obtained from the SDCard next time? This is also a practice. I have read it and many applications adopt this method. Using LRU and other algorithms can ensure that the space occupied by sdcard is only a small part, this not only ensures image loading, reduces traffic, but also makes the SDCard space only occupy a small part. Another approach is to store resources directly in the memory and set the expiration time and LRU rules.
Save sdcard:
To open up a certain amount of space on the sdcard, you must first determine whether the remaining space on the sdcard is sufficient. If there is enough space, you can open up some space, such as 10 M.
When you need to obtain an image, you must first find it from the directory on the sdcard. If you find the image, use the image and update the last time the image was used. If not found, download through URL
Go to the server to download the image. If the download is successful, place it on the sdcard and use it. If the download fails, a Retry Mechanism should be available. For example, three times.
After the download is successful and saved to the sdcard, You need to determine whether the 10 MB space is used up. If the space is not used up, save it. If the space is insufficient, delete some resources that have not been used recently according to the LRU rule.
Save the image to the SD card
Java code:
- Private void saveBmpToSd (Bitmap bm, Stringurl ){
- If (bm = null ){
- Log. w (TAG, "trying to savenull bitmap ");
- Return;
- }
- // Determine the space on the sdcard
- If (FREE_SD_SPACE_NEEDED_TO_CACHE> freeSpaceOnSd ()){
- Log. w (TAG, "Low free space onsd, do not cache ");
- Return;
- }
- String filename = convertUrlToFileName (url );
- String dir = getDirectory (filename );
- File file = new File (dir + "/" + filename );
- Try {
- File. createNewFile ();
- OutputStream outStream = newFileOutputStream (file );
- Bm. compress (Bitmap. CompressFormat. JPEG, 100, outStream );
- OutStream. flush ();
- OutStream. close ();
- Log. I (TAG, "Image saved tosd ");
- } Catch (FileNotFoundException e ){
- Log. w (TAG, "FileNotFoundException ");
- } Catch (IOException e ){
- Log. w (TAG, "IOException ");
- }
- }
Copy code
Calculate the space on the sdcard:
Java code:
- /**
- * Calculate the remaining space on the sdcard
- * @ Return
- */
- Private int freeSpaceOnSd (){
- StatFs stat = newStatFs (Environment. getExternalStorageDirectory (). getPath ());
- Double sdFreeMB = (double) stat. getAvailableBlocks () * (double) stat. getBlockSize ()/MB;
- Return (int) sdFreeMB;
- }
Copy code
Series of Android remote image acquisition and local cache (2) Post link http://www.eoeandroid.com/thread-98449-1-1.html
Series of Android remote image acquisition and local cache (3) Post link http://www.eoeandroid.com/thread-98450-1-1.html