public class ImageMemoryCache {/*** 从内存读取数据速度是最快的为了更大限度使用内存这里使用了两层缓存。* 硬引用缓存不会轻易被回收用来保存常用数据不常用的转入软引用缓存。*/private static final int SOFT_CACHE_SIZE 15; //软引用缓存容量private static LruCache mLruCache; //硬引用缓存private static LinkedHashMap package bitmap.cache;import java.io.FilterInputStream;import java.io.IOException;import java.io.InputStream;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.HttpStatus;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.util.Log;public class ImageGetFromHttp {private static final String LOG_TAG "ImageGetFromHttp";public static Bitmap downloadBitmap(String url) {final HttpClient client new DefaultHttpClient();final HttpGet getRequest new HttpGet(url);try {HttpResponse response client.execute(getRequest);final int statusCode response.getStatusLine().getStatusCode();if (statusCode ! HttpStatus.SC_OK) {Log.w(LOG_TAG, "Error " statusCode " while retrieving bitmap from " url);return null;}final HttpEntity entity response.getEntity();if (entity ! null) {InputStream inputStream null;try {inputStream entity.getContent();FilterInputStream fit new FlushedInputStream(inputStream);return BitmapFactory.decodeStream(fit);} finally {if (inputStream ! null) {inputStream.close();inputStream null;}entity.consumeContent();}}} catch (IOException e) {getRequest.abort();Log.w(LOG_TAG, "I/O error while retrieving bitmap from " url, e);} catch (IllegalStateException e) {getRequest.abort();Log.w(LOG_TAG, "Incorrect URL: " url);} catch (Exception e) {getRequest.abort();Log.w(LOG_TAG, "Error while retrieving bitmap from " url, e);} finally {client.getConnectionManager().shutdown();}return null;}/** An InputStream that skips the exact number of bytes provided, unless it reaches EOF.*/static class FlushedInputStream extends FilterInputStream {public FlushedInputStream(InputStream inputStream) {super(inputStream);}Overridepublic long skip(long n) throws IOException {long totalBytesSkipped 0L;while (totalBytesSkipped < n) {long bytesSkipped in.skip(n - totalBytesSkipped);if (bytesSkipped 0L) {int b read();if (b <0) {break; // we reached EOF} else {bytesSkipped 1; // we read one byte }}totalBytesSkipped bytesSkipped;}return totalBytesSkipped;}}} public class GetBitmap {/*** 获得一张图片,从三个地方获取,首先是内存缓存,然后是文件缓存,最后从网络获取 ***/public static Bitmap getBitmap(String url) {// 从内存缓存中获取图片Bitmap result TestApp.memoryCache.getBitmapFromCache(url);if (result null) {// 文件缓存中获取result TestApp.fileCache.getImage(url);if (result null) {// 从网络获取result ImageGetFromHttp.downloadBitmap(url);if (result ! null) {TestApp.fileCache.saveBitmap(result, url);TestApp.memoryCache.addBitmapToCache(url, result);}} else {// 添加到内存缓存 TestApp.memoryCache.addBitmapToCache(url, result);}}return result;}} 就这样了啊要记得用线程加载啊否则就。。。。。。。。。。。你懂的 转:https://www.cnblogs.com/jinglingJuly/archive/2013/05/21/3089951.html