我正在使用ImageDownloader.java文件从服务器下载/缓存图像,但当我尝试从resize API下载图像时无法下载图像,即http://www.xxxxx.com/wp-content/image2png.php?src=/2013/11/GAP.jpg&mode=fixed&form=jpg
ImageDownloader.java完美地处理以.jpg和.png扩展名结尾的URL。
ImageDownloaded.java
公共类ImageDownloader {
public ImageDownloader(String _FileLocation) {
super();
this._FileLocation = _FileLocation;
}
public static final int _FULLDOWNLOAD = 0;
public static final int _FULLDOWNLOAD_WITHOUTCACHE = 1;
public static final int _SCALEDDOWNLOAD = 2;
private SaveFileToHardDrive imageByteDownloader = new SaveFileToHardDrive();
private String _FileLocation = "";
public enum Mode {
NO_ASYNC_TASK, NO_DOWNLOADED_DRAWABLE, CORRECT
}
private Mode mode = Mode.CORRECT;
public void download(int downloadType, String url, ImageView imageView, View progressBar) {
resetPurgeTimer();
if(!url.contains("http")){
url = "http:" + url;
}
ImageData bitmap = getBitmapFromCache(url);
if (bitmap == null || bitmap.getBitMap() == null) {
forceDownload(downloadType, url, imageView, progressBar);
} else {
cancelPotentialDownload(url, imageView);
try {
imageView.setImageURI(bitmap.getBitMap());
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
if (progressBar != null)
progressBar.setVisibility(View.GONE);
}
}
private void forceDownload(int downloadType, String url, ImageView imageView, View progressBar) {
// State sanity: url is guaranteed to never be null in DownloadedDrawable and cache keys.
if (url == null) {
imageView.setImageDrawable(null);
if (progressBar != null)
progressBar.setVisibility(View.GONE);
return;
}
if (cancelPotentialDownload(url, imageView)) {
switch (mode) {
case CORRECT:
try {
BitmapDownloaderTask task = new BitmapDownloaderTask(imageView, progressBar);
DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task);
imageView.setImageDrawable(downloadedDrawable);
// imageView.setMinimumHeight(156);
Object[] obj = { url, downloadType };
// task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, obj);
task.execute(obj);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
}
private static boolean cancelPotentialDownload(String url, ImageView imageView) {
BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageView);
if (bitmapDownloaderTask != null) {
String bitmapUrl = bitmapDownloaderTask.url;
if ((bitmapUrl == null) || (!bitmapUrl.equals(url))) {
bitmapDownloaderTask.cancel(true);
} else {
// The same URL is already being downloaded.
return false;
}
}
return true;
}
private static BitmapDownloaderTask getBitmapDownloaderTask(ImageView imageView) {
if (imageView != null) {
Drawable drawable = imageView.getDrawable();
if (drawable instanceof DownloadedDrawable) {
DownloadedDrawable downloadedDrawable = (DownloadedDrawable) drawable;
return downloadedDrawable.getBitmapDownloaderTask();
}
}
return null;
}
String downloadBitmap(String url, int downloadType) {
try {
// url = new Utility().stripWrongContent(url, " ", "%20");
String imageByte = imageByteDownloader.fetchActualImagePath(url, _FileLocation, SaveFileToHardDrive._RegulerImagesDierctory);
if (imageByte != null && !imageByte.contains("file://null")) {
return imageByte;
}
} catch (OutOfMemoryError e) {
e.printStackTrace();
clearCache();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
static class FlushedInputStream extends FilterInputStream {
public FlushedInputStream(InputStream inputStream) {
super(inputStream);
}
@Override
public 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;
}
}
class BitmapDownloaderTask extends AsyncTask<Object, Void, ImageData> {
private String url;
private int downloadType = 0;
private final WeakReference<ImageView> imageViewReference;
protected WeakReference<View> progRef;
public BitmapDownloaderTask(ImageView imageView, View progressBar) {
imageViewReference = new WeakReference<ImageView>(imageView);
progRef = new WeakReference<View>(progressBar);
}
/**
* Actual download method.
*/
@Override
protected ImageData doInBackground(Object... params) {
url = (String) params[0];
downloadType = (Integer) params[1];
ImageData imData = new ImageData();
imData.setBitMap(downloadBitmap(url, downloadType));
imData.setDownloadType(downloadType);
return imData;
}
@Override
protected void onPostExecute(ImageData bitmap) {
View prog = progRef.get();
if (isCancelled()) {
bitmap = null;
if (prog != null)
prog.setVisibility(View.GONE);
}
addBitmapToCache(url, bitmap);
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageView);
// Change bitmap only if this process is still associated with it
// Or if we don't use any bitmap to task association (NO_DOWNLOADED_DRAWABLE mode)
if ((this == bitmapDownloaderTask) || (mode != Mode.CORRECT)) {
try {
if (bitmap.getBitMap() != null)
imageView.setImageURI(bitmap.getBitMap());
else
imageView.setVisibility(View.GONE);
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
if (prog != null)
prog.setVisibility(View.GONE);
}
}
}
}
static class DownloadedDrawable extends ColorDrawable {
private final WeakReference<BitmapDownloaderTask> bitmapDownloaderTaskReference;
public DownloadedDrawable(BitmapDownloaderTask bitmapDownloaderTask) {
super(Color.TRANSPARENT);
bitmapDownloaderTaskReference = new WeakReference<BitmapDownloaderTask>(bitmapDownloaderTask);
}
public BitmapDownloaderTask getBitmapDownloaderTask() {
return bitmapDownloaderTaskReference.get();
}
}
public void setMode(Mode mode) {
this.mode = mode;
clearCache();
}
private static final int HARD_CACHE_CAPACITY = 15;
private static final int DELAY_BEFORE_PURGE = 10 * 1000; // in milliseconds
// Hard cache, with a fixed maximum capacity and a life duration
private final HashMap<String, ImageData> sHardBitmapCache = new LinkedHashMap<String, ImageData>(HARD_CACHE_CAPACITY / 2, 0.75f, true) {
@Override
protected boolean removeEldestEntry(LinkedHashMap.Entry<String, ImageData> eldest) {
if (size() > HARD_CACHE_CAPACITY) {
// Entries push-out of hard reference cache are transferred to soft reference cache
sSoftBitmapCache.put(eldest.getKey(), new SoftReference<ImageData>(eldest.getValue()));
return true;
} else
return false;
}
};
// Soft cache for bitmaps kicked out of hard cache
private final static ConcurrentHashMap<String, SoftReference<ImageData>> sSoftBitmapCache = new ConcurrentHashMap<String, SoftReference<ImageData>>(HARD_CACHE_CAPACITY / 2);
private final Handler purgeHandler = new Handler();
private final Runnable purger = new Runnable() {
public void run() {
clearCache();
}
};
private void addBitmapToCache(String url, ImageData bitmap) {
if (bitmap != null) {
synchronized (sHardBitmapCache) {
sHardBitmapCache.put(url, bitmap);
}
}
}
private ImageData getBitmapFromCache(String url) {
try {
// First try the hard reference cache
synchronized (sHardBitmapCache) {
final ImageData bitmap = sHardBitmapCache.get(url);
if (bitmap != null) {
// Bitmap found in hard cache
// Move element to first position, so that it is removed last
sHardBitmapCache.remove(url);
sHardBitmapCache.put(url, bitmap);
return bitmap;
}
}
// Then try the soft reference cache
SoftReference<ImageData> bitmapReference = sSoftBitmapCache.get(url);
if (bitmapReference != null) {
final ImageData bitmap = bitmapReference.get();
if (bitmap != null) {
// Bitmap found in soft cache
return bitmap;
} else {
// Soft reference has been Garbage Collected
sSoftBitmapCache.remove(url);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void clearCache() {
try {
sHardBitmapCache.clear();
sSoftBitmapCache.clear();
} catch (Exception e) {
e.printStackTrace();
}
}
private void resetPurgeTimer() {
purgeHandler.removeCallbacks(purger);
purgeHandler.postDelayed(purger, DELAY_BEFORE_PURGE);
}
}