Android Remote Image Retrieval and local cache (3)

Source: Internet
Author: User

Obtain data from the cache:

Java code:

  1. /**
  2. * Retrieving images from the cache
  3. */
  4. Private Bitmap getBitmapFromCache (Stringurl ){
  5. // Obtain it from the mHardBitmapCache cache first
  6. Synchronized (mHardBitmapCache ){
  7. Final Bitmap bitmap = mHardBitmapCache. get (url );
  8. If (bitmap! = Null ){
  9. // If it is found, move the element to the beginning of linkedhashmap to ensure that it is finally deleted in the LRU algorithm.
  10. MHardBitmapCache. remove (url );
  11. MHardBitmapCache. put (url, bitmap );
  12. Return bitmap;
  13. }
  14. }
  15. // If mHardBitmapCache cannot be found, find it in mSoftBitmapCache
  16. SoftReference <Bitmap> bitmapReference = mSoftBitmapCache. get (url );
  17. If (bitmapReference! = Null ){
  18. Final Bitmap bitmap = bitmapReference. get ();
  19. If (bitmap! = Null ){
  20. Return bitmap;
  21. } Else {
  22. MSoftBitmapCache. remove (url );
  23. }
  24. }
  25. Return null;
  26. }

Copy code

If the cache does not exist, you can only download it from the server:

Java code:

  1. /**
  2. * Asynchronous image download
  3. */
  4. Class ImageDownloaderTask extendsAsyncTask <String, Void, Bitmap> {
  5. Private static final int IO_BUFFER_SIZE = 4*1024;
  6. Private String url;
  7. Private finalWeakReference <ImageView> imageViewReference;
  8. Public ImageDownloaderTask (ImageViewimageView ){
  9. ImageViewReference = newWeakReference <ImageView> (imageView );
  10. }
  11. @ Override
  12. Protected BitmapdoInBackground (String... params ){
  13. Final AndroidHttpClient client = AndroidHttpClient. newInstance ("Android ");
  14. Url = params [0];
  15. Final HttpGet getRequest = newHttpGet (url );
  16. Try {
  17. HttpResponse response implements client.exe cute (getRequest );
  18. Final int statusCode = response. getStatusLine (). getStatusCode ();
  19. If (statusCode! = HttpStatus. SC _ OK ){
  20. Log. w (TAG, "An error occurred while downloading images from" + url + !, Error Code: "+ statusCode );
  21. Return null;
  22. }
  23. Final HttpEntity entity = response. getEntity ();
  24. If (entity! = Null ){
  25. InputStream inputStream = null;
  26. OutputStream outputStream = null;
  27. Try {
  28. InputStream = entity. getContent ();
  29. FinalByteArrayOutputStream dataStream = new ByteArrayOutputStream ();
  30. OutputStream = newBufferedOutputStream (dataStream, IO_BUFFER_SIZE );
  31. Copy (inputStream, outputStream );
  32. OutputStream. flush ();
  33. Final byte [] data = dataStream. toByteArray ();
  34. Final Bitmap bitmap = BitmapFactory. decodeByteArray (data, 0, data. length );
  35. Return bitmap;
  36. } Finally {
  37. If (inputStream! = Null ){
  38. InputStream. close ();
  39. }
  40. If (outputStream! = Null ){
  41. OutputStream. close ();
  42. }
  43. Entity. consumeContent ();
  44. }
  45. }
  46. } Catch (IOException e ){
  47. GetRequest. abort ();
  48. Log. w (TAG, "I/O errorwhile retrieving bitmap from" + url, e );
  49. } Catch (IllegalStateException e ){
  50. GetRequest. abort ();
  51. Log. w (TAG, "Incorrect URL:" + url );
  52. } Catch (Exception e ){
  53. GetRequest. abort ();
  54. Log. w (TAG, "Error whileretrieving bitmap from" + url, e );
  55. } Finally {
  56. If (client! = Null ){
  57. Client. close ();
  58. }
  59. }
  60. Return null;
  61. }

Copy code

Series of Android remote image acquisition and local cache (1) Post link http://www.eoeandroid.com/thread-98446-1-1.html
Series of Android remote image acquisition and local cache (2) Post link http://www.eoeandroid.com/thread-98449-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.