Android Remote Image Retrieval and local cache (1)

Source: Internet
Author: User

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:

  1. Private void saveBmpToSd (Bitmap bm, Stringurl ){
  2. If (bm = null ){
  3. Log. w (TAG, "trying to savenull bitmap ");
  4. Return;
  5. }
  6. // Determine the space on the sdcard
  7. If (FREE_SD_SPACE_NEEDED_TO_CACHE> freeSpaceOnSd ()){
  8. Log. w (TAG, "Low free space onsd, do not cache ");
  9. Return;
  10. }
  11. String filename = convertUrlToFileName (url );
  12. String dir = getDirectory (filename );
  13. File file = new File (dir + "/" + filename );
  14. Try {
  15. File. createNewFile ();
  16. OutputStream outStream = newFileOutputStream (file );
  17. Bm. compress (Bitmap. CompressFormat. JPEG, 100, outStream );
  18. OutStream. flush ();
  19. OutStream. close ();
  20. Log. I (TAG, "Image saved tosd ");
  21. } Catch (FileNotFoundException e ){
  22. Log. w (TAG, "FileNotFoundException ");
  23. } Catch (IOException e ){
  24. Log. w (TAG, "IOException ");
  25. }
  26. }

Copy code

Calculate the space on the sdcard:

Java code:

  1. /**
  2. * Calculate the remaining space on the sdcard
  3. * @ Return
  4. */
  5. Private int freeSpaceOnSd (){
  6. StatFs stat = newStatFs (Environment. getExternalStorageDirectory (). getPath ());
  7. Double sdFreeMB = (double) stat. getAvailableBlocks () * (double) stat. getBlockSize ()/MB;
  8. Return (int) sdFreeMB;
  9. }

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

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.