Obtain data from the cache:
Java code:
- /**
- * Retrieving images from the cache
- */
- Private Bitmap getBitmapFromCache (Stringurl ){
- // Obtain it from the mHardBitmapCache cache first
- Synchronized (mHardBitmapCache ){
- Final Bitmap bitmap = mHardBitmapCache. get (url );
- If (bitmap! = Null ){
- // If it is found, move the element to the beginning of linkedhashmap to ensure that it is finally deleted in the LRU algorithm.
- MHardBitmapCache. remove (url );
- MHardBitmapCache. put (url, bitmap );
- Return bitmap;
- }
- }
- // If mHardBitmapCache cannot be found, find it in mSoftBitmapCache
- SoftReference <Bitmap> bitmapReference = mSoftBitmapCache. get (url );
- If (bitmapReference! = Null ){
- Final Bitmap bitmap = bitmapReference. get ();
- If (bitmap! = Null ){
- Return bitmap;
- } Else {
- MSoftBitmapCache. remove (url );
- }
- }
- Return null;
- }
Copy code
If the cache does not exist, you can only download it from the server:
Java code:
- /**
- * Asynchronous image download
- */
- Class ImageDownloaderTask extendsAsyncTask <String, Void, Bitmap> {
- Private static final int IO_BUFFER_SIZE = 4*1024;
- Private String url;
- Private finalWeakReference <ImageView> imageViewReference;
- Public ImageDownloaderTask (ImageViewimageView ){
- ImageViewReference = newWeakReference <ImageView> (imageView );
- }
- @ Override
- Protected BitmapdoInBackground (String... params ){
- Final AndroidHttpClient client = AndroidHttpClient. newInstance ("Android ");
- Url = params [0];
- Final HttpGet getRequest = newHttpGet (url );
- Try {
- HttpResponse response implements client.exe cute (getRequest );
- Final int statusCode = response. getStatusLine (). getStatusCode ();
- If (statusCode! = HttpStatus. SC _ OK ){
- Log. w (TAG, "An error occurred while downloading images from" + url + !, Error Code: "+ statusCode );
- Return null;
- }
- Final HttpEntity entity = response. getEntity ();
- If (entity! = Null ){
- InputStream inputStream = null;
- OutputStream outputStream = null;
- Try {
- InputStream = entity. getContent ();
- FinalByteArrayOutputStream dataStream = new ByteArrayOutputStream ();
- OutputStream = newBufferedOutputStream (dataStream, IO_BUFFER_SIZE );
- Copy (inputStream, outputStream );
- OutputStream. flush ();
- Final byte [] data = dataStream. toByteArray ();
- Final Bitmap bitmap = BitmapFactory. decodeByteArray (data, 0, data. length );
- Return bitmap;
- } Finally {
- If (inputStream! = Null ){
- InputStream. close ();
- }
- If (outputStream! = Null ){
- OutputStream. close ();
- }
- Entity. consumeContent ();
- }
- }
- } Catch (IOException e ){
- GetRequest. abort ();
- Log. w (TAG, "I/O errorwhile retrieving bitmap from" + url, e );
- } Catch (IllegalStateException e ){
- GetRequest. abort ();
- Log. w (TAG, "Incorrect URL:" + url );
- } Catch (Exception e ){
- GetRequest. abort ();
- Log. w (TAG, "Error whileretrieving bitmap from" + url, e );
- } Finally {
- If (client! = Null ){
- Client. close ();
- }
- }
- Return null;
- }
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