Created AsnycTask for downloading thumbnails from the miroguide
This commit is contained in:
parent
fadb0de18c
commit
7ada4c2efb
@ -16,8 +16,8 @@ import de.danoeh.antennapod.util.BitmapDecoder;
|
|||||||
public abstract class BitmapDecodeWorkerTask extends
|
public abstract class BitmapDecodeWorkerTask extends
|
||||||
AsyncTask<Void, Void, Void> {
|
AsyncTask<Void, Void, Void> {
|
||||||
|
|
||||||
private int PREFERRED_LENGTH;
|
protected int PREFERRED_LENGTH;
|
||||||
|
|
||||||
public static final int LENGTH_BASE_COVER = 200;
|
public static final int LENGTH_BASE_COVER = 200;
|
||||||
public static final int LENGTH_BASE_THUMBNAIL = 100;
|
public static final int LENGTH_BASE_THUMBNAIL = 100;
|
||||||
|
|
||||||
@ -28,7 +28,7 @@ public abstract class BitmapDecodeWorkerTask extends
|
|||||||
|
|
||||||
protected int baseLength;
|
protected int baseLength;
|
||||||
|
|
||||||
private String fileUrl;
|
protected String fileUrl;
|
||||||
|
|
||||||
public BitmapDecodeWorkerTask(ImageView target, String fileUrl, int length) {
|
public BitmapDecodeWorkerTask(ImageView target, String fileUrl, int length) {
|
||||||
super();
|
super();
|
||||||
@ -61,27 +61,6 @@ public abstract class BitmapDecodeWorkerTask extends
|
|||||||
super.onPreExecute();
|
super.onPreExecute();
|
||||||
}
|
}
|
||||||
|
|
||||||
private int calculateSampleSize(int width, int height) {
|
|
||||||
int max = Math.max(width, height);
|
|
||||||
if (max < PREFERRED_LENGTH) {
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
// find first sample size where max / sampleSize <
|
|
||||||
// PREFERRED_LENGTH
|
|
||||||
for (int sampleSize = 1, power = 0;; power++, sampleSize = (int) Math
|
|
||||||
.pow(2, power)) {
|
|
||||||
int newLength = max / sampleSize;
|
|
||||||
if (newLength <= PREFERRED_LENGTH) {
|
|
||||||
if (newLength > 0) {
|
|
||||||
return sampleSize;
|
|
||||||
} else {
|
|
||||||
return sampleSize - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... params) {
|
protected Void doInBackground(Void... params) {
|
||||||
File f = null;
|
File f = null;
|
||||||
@ -104,13 +83,13 @@ public abstract class BitmapDecodeWorkerTask extends
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void onInvalidFileUrl() {
|
protected void onInvalidFileUrl() {
|
||||||
Log.e(TAG, "FeedImage has no valid file url. Using default image");
|
Log.e(TAG, "FeedImage has no valid file url. Using default image");
|
||||||
bitmap = BitmapFactory.decodeResource(target.getResources(),
|
bitmap = BitmapFactory.decodeResource(target.getResources(),
|
||||||
R.drawable.default_cover);
|
R.drawable.default_cover);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void storeBitmapInCache(Bitmap bitmap);
|
protected abstract void storeBitmapInCache(Bitmap bitmap);
|
||||||
|
|
||||||
@SuppressLint("NewApi")
|
@SuppressLint("NewApi")
|
||||||
|
@ -0,0 +1,123 @@
|
|||||||
|
package de.danoeh.antennapod.asynctask;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
import com.jakewharton.DiskLruCache;
|
||||||
|
import com.jakewharton.DiskLruCache.Editor;
|
||||||
|
|
||||||
|
import de.danoeh.antennapod.AppConfig;
|
||||||
|
import de.danoeh.antennapod.PodcastApp;
|
||||||
|
import de.danoeh.antennapod.miroguide.model.MiroChannel;
|
||||||
|
import de.danoeh.antennapod.util.BitmapDecoder;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.pm.PackageManager.NameNotFoundException;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.widget.ImageView;
|
||||||
|
|
||||||
|
/** Downlods thumbnails from the MiroGuide and stores them in a DiskLruCache */
|
||||||
|
public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
|
||||||
|
private static final String TAG = "MiroGuideThumbnailDownloader";
|
||||||
|
|
||||||
|
private static final String CACHE_DIR = "miroguide_thumbnails";
|
||||||
|
private static final int CACHE_SIZE = 20 * 1024 * 1024;
|
||||||
|
private static final int VALUE_SIZE = 500 * 1024;
|
||||||
|
|
||||||
|
private Exception exception;
|
||||||
|
|
||||||
|
private MiroChannel miroChannel;
|
||||||
|
|
||||||
|
public MiroGuideThumbnailDownloader(ImageView target,
|
||||||
|
MiroChannel miroChannel, int length) {
|
||||||
|
super(target, miroChannel.getThumbnailUrl(), length);
|
||||||
|
this.miroChannel = miroChannel;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static DiskLruCache openThubmnailDiskCache()
|
||||||
|
throws NameNotFoundException, IOException {
|
||||||
|
Context appContext = PodcastApp.getInstance();
|
||||||
|
return DiskLruCache.open(
|
||||||
|
appContext.getExternalFilesDir(CACHE_DIR),
|
||||||
|
appContext.getPackageManager().getPackageInfo(
|
||||||
|
appContext.getPackageName(), 0).versionCode,
|
||||||
|
VALUE_SIZE, CACHE_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Void doInBackground(Void... params) {
|
||||||
|
// Download file to cache folder
|
||||||
|
URL url = null;
|
||||||
|
try {
|
||||||
|
url = new URL(fileUrl);
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
File destination = new File(PodcastApp.getInstance().getCacheDir(),
|
||||||
|
Integer.toString(fileUrl.hashCode()));
|
||||||
|
try {
|
||||||
|
DiskLruCache diskCache = openThubmnailDiskCache();
|
||||||
|
Editor editor = diskCache.edit(fileUrl);
|
||||||
|
if (editor != null) {
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url
|
||||||
|
.openConnection();
|
||||||
|
byte inputBuffer[] = new byte[10 * 1024];
|
||||||
|
InputStream input = new BufferedInputStream(
|
||||||
|
connection.getInputStream());
|
||||||
|
FileOutputStream output = new FileOutputStream(destination);
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
while ((count = input.read(inputBuffer, 0, 10 * 1024)) != -1) {
|
||||||
|
output.write(inputBuffer, 0, count);
|
||||||
|
}
|
||||||
|
output.close();
|
||||||
|
|
||||||
|
// Get a smaller version of the bitmap and store it inside the
|
||||||
|
// LRU
|
||||||
|
// Cache
|
||||||
|
Bitmap bitmap = BitmapDecoder.decodeBitmap(PREFERRED_LENGTH,
|
||||||
|
fileUrl);
|
||||||
|
if (bitmap != null) {
|
||||||
|
OutputStream imageOut = editor.newOutputStream(0);
|
||||||
|
bitmap.compress(Bitmap.CompressFormat.PNG, 80, imageOut);
|
||||||
|
editor.commit();
|
||||||
|
storeBitmapInCache(bitmap);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (AppConfig.DEBUG)
|
||||||
|
Log.d(TAG, "No editor object available");
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
} catch (NameNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
|
if (destination.exists()) {
|
||||||
|
destination.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean tagsMatching(ImageView target) {
|
||||||
|
return target.getTag() == null || target.getTag() == miroChannel;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void storeBitmapInCache(Bitmap bitmap) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user