Android image Cache

Source: Internet
Author: User

    Blog type: 

  • Android
Androidjava code
  1. Later, I found this method on the foreign guy's website.
  2. // Obtain and cache images from the network, and share the cache with the browser
  3. URL url = new URL (imageurl );
  4. Urlconnection connection = URL. openconnection ();
  5. Connection. setusecaches (true );
  6. Bitmap bitmap = bitmapfactory. decodestream (inputstream) connection. getcontent ());
  7. However, experiments have proved that images cached in the above method can be quickly loaded only with certain network support;
  8. The following method is better. After caching, the image can be obtained immediately even if the network is poor.
  9. Next is another article
  10. [Transfer]
  11. At present, many commercial applications are involved in reading image data from the network. To save user traffic, applications generally cache images. Image cache is generally divided into memory cache and external cache. The memory cache uses the Java cache mechanism. After the program exits completely, the memory space of the cache may be occupied by other applications and thus lost. The external storage cache is generally stored in the unique access space or SD card of the program. The resources stored in the SD card are public resources, and other programs can also access the OSS, there is no standard mechanism for users to forcibly clear the cache. In summary, this document places cached images in a specific space of the program, and other applications cannot access the cache. Users can clear the cache in the "Clear Data" option in application management.
  12. This article provides three cache policies: (1) LRU algorithm, fixed number of cached images (max_num). When the number of cached images exceeds max_num, the minimum number of recently used images in the cache is deleted. (2) FTU algorithm, fixed the cache time limit for each image, counted from the last use, and deleted after the time limit is exceeded. (3) The FMU algorithm is used to fix a certain size of storage space in the memory. When the storage space exceeds the fixed space, the maximum size of images in the cache is deleted. You only need to pass the image URL to the method body.
  13. Usage:
  14. 1. Import jar;
  15. 2. Obtain the service;
  16. 3. Submit the URL and submit it to the program to determine whether to download the URL.
  17. The attachment is a runnable project with Jar

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.