Thumbnail download is now partly working

This commit is contained in:
daniel oeh 2012-08-03 19:48:59 +02:00
parent 93812142da
commit e170605ebc
8 changed files with 73 additions and 51 deletions

View File

@ -6,14 +6,13 @@
<ImageView <ImageView
android:id="@+id/imgvChannelimage" android:id="@+id/imgvChannelimage"
android:src="@drawable/default_cover" android:layout_width="40dip"
android:layout_width="55dip" android:layout_height="40dip"
android:layout_height="55dip"
android:layout_alignParentLeft="true" android:layout_alignParentLeft="true"
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:layout_marginLeft="1dip" android:layout_marginLeft="1dip"
android:layout_marginRight="4dip" android:layout_marginRight="4dip"
android:cropToPadding="true" /> android:src="@drawable/default_cover" />
<LinearLayout <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"

View File

@ -19,6 +19,7 @@ import android.widget.ImageView;
import com.jakewharton.DiskLruCache; import com.jakewharton.DiskLruCache;
import com.jakewharton.DiskLruCache.Snapshot; import com.jakewharton.DiskLruCache.Snapshot;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.PodcastApp; import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.R; import de.danoeh.antennapod.R;
import de.danoeh.antennapod.feed.FeedImage; import de.danoeh.antennapod.feed.FeedImage;
@ -159,6 +160,7 @@ public class FeedImageLoader {
.getThumbnailUrl()); .getThumbnailUrl());
if (bitmap == null) { if (bitmap == null) {
boolean isInDiskCache = false; boolean isInDiskCache = false;
/*
try { try {
isInDiskCache = isInThumbnailDiskCache(channel isInDiskCache = isInThumbnailDiskCache(channel
.getThumbnailUrl()); .getThumbnailUrl());
@ -166,10 +168,13 @@ public class FeedImageLoader {
e.printStackTrace(); e.printStackTrace();
Log.e(TAG, "Error when trying to read disk cache"); Log.e(TAG, "Error when trying to read disk cache");
} }
*/
if (isInDiskCache) { if (isInDiskCache) {
executor.submit(new MiroGuideDiskCacheLoader(handler, executor.submit(new MiroGuideDiskCacheLoader(handler,
target, channel, LENGTH_BASE_THUMBNAIL)); target, channel, LENGTH_BASE_THUMBNAIL));
} else { } else {
if (AppConfig.DEBUG) Log.d(TAG, "Starting new thumbnail download");
executor.submit(new MiroGuideThumbnailDownloader(handler, executor.submit(new MiroGuideThumbnailDownloader(handler,
target, channel, LENGTH_BASE_THUMBNAIL)); target, channel, LENGTH_BASE_THUMBNAIL));
} }

View File

@ -1,6 +1,7 @@
package de.danoeh.antennapod.asynctask; package de.danoeh.antennapod.asynctask;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
@ -9,6 +10,7 @@ import java.io.OutputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection;
import com.jakewharton.DiskLruCache; import com.jakewharton.DiskLruCache;
import com.jakewharton.DiskLruCache.Editor; import com.jakewharton.DiskLruCache.Editor;
@ -19,6 +21,7 @@ import de.danoeh.antennapod.miroguide.model.MiroChannel;
import de.danoeh.antennapod.util.BitmapDecoder; import de.danoeh.antennapod.util.BitmapDecoder;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler; import android.os.Handler;
import android.util.Log; import android.util.Log;
import android.widget.ImageView; import android.widget.ImageView;
@ -39,7 +42,7 @@ public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
@Override @Override
protected void onPostExecute() { protected void onPostExecute() {
if (exception != null) { if (exception == null) {
super.onPostExecute(); super.onPostExecute();
} else { } else {
Log.e(TAG, "Failed to download thumbnail"); Log.e(TAG, "Failed to download thumbnail");
@ -58,40 +61,43 @@ public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
File destination = new File(PodcastApp.getInstance().getCacheDir(), File destination = new File(PodcastApp.getInstance().getCacheDir(),
Integer.toString(fileUrl.hashCode())); Integer.toString(fileUrl.hashCode()));
try { try {
DiskLruCache diskCache = FeedImageLoader.openThubmnailDiskCache(); /*
Editor editor = diskCache.edit(fileUrl); * DiskLruCache diskCache =
if (editor != null) { * FeedImageLoader.openThubmnailDiskCache(); Editor editor =
HttpURLConnection connection = (HttpURLConnection) url * diskCache.edit(fileUrl);
.openConnection(); */
byte inputBuffer[] = new byte[10 * 1024]; if (AppConfig.DEBUG) Log.d(TAG, "Downloading " + fileUrl);
InputStream input = new BufferedInputStream( URLConnection connection = url.openConnection();
connection.getInputStream()); connection.connect();
FileOutputStream output = new FileOutputStream(destination); byte inputBuffer[] = new byte[1024];
BufferedInputStream input = new BufferedInputStream(
connection.getInputStream());
FileOutputStream output = new FileOutputStream(destination);
int count = 0; int count = 0;
while ((count = input.read(inputBuffer, 0, 10 * 1024)) != -1) { while ((count = input.read(inputBuffer)) != -1) {
output.write(inputBuffer, 0, count); output.write(inputBuffer, 0, count);
}
output.close();
connection.disconnect();
if (AppConfig.DEBUG) Log.d(TAG, "MiroGuide thumbnail downloaded");
// 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) if (AppConfig.DEBUG)
Log.d(TAG, "No editor object available"); Log.d(TAG, "" + count);
} }
output.close();
if (AppConfig.DEBUG)
Log.d(TAG, "MiroGuide thumbnail downloaded");
// Get a smaller version of the bitmap and store it inside the
// LRU
// Cache
Bitmap bitmap = BitmapDecoder.decodeBitmap(PREFERRED_LENGTH,
destination.getPath());
if (bitmap != null) {
// OutputStream imageOut = editor.newOutputStream(0);
// bitmap.compress(Bitmap.CompressFormat.PNG, 80, imageOut);
// editor.commit();
storeBitmapInCache(bitmap);
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
miroChannel.setThumbnailUrl(null);
endBackgroundTask(); endBackgroundTask();
} finally { } finally {
if (destination.exists()) { if (destination.exists()) {

View File

@ -228,7 +228,7 @@ public class MiroGuideChannellistFragment extends SherlockListFragment {
exception = e; exception = e;
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
service.close(); //service.close();
} }
return null; return null;
} }

View File

@ -19,7 +19,7 @@ import android.net.http.AndroidHttpClient;
/** Executes HTTP requests and returns the results. */ /** Executes HTTP requests and returns the results. */
public class MiroGuideConnector { public class MiroGuideConnector {
private AndroidHttpClient httpClient; private HttpClient httpClient;
private static final String HOST_URL = "https://www.miroguide.com/api/"; private static final String HOST_URL = "https://www.miroguide.com/api/";
private static final String PATH_GET_CHANNELS = "get_channels"; private static final String PATH_GET_CHANNELS = "get_channels";
@ -27,11 +27,11 @@ public class MiroGuideConnector {
private static final String PATH_GET_CHANNEL = "get_channel"; private static final String PATH_GET_CHANNEL = "get_channel";
public MiroGuideConnector() { public MiroGuideConnector() {
httpClient = AndroidHttpClient.newInstance(null); httpClient = new DefaultHttpClient();
} }
public void shutdown() { public void shutdown() {
httpClient.close(); httpClient.getConnectionManager().shutdown();
} }
private Uri.Builder getBaseURIBuilder(String path) { private Uri.Builder getBaseURIBuilder(String path) {

View File

@ -17,6 +17,8 @@ import de.danoeh.antennapod.miroguide.model.MiroItem;
/** Provides methods to communicate with the Miroguide API on an abstract level. */ /** Provides methods to communicate with the Miroguide API on an abstract level. */
public class MiroGuideService { public class MiroGuideService {
private static final String TAG = "MiroGuideService";
public static final int DEFAULT_CHANNEL_LIMIT = 20; public static final int DEFAULT_CHANNEL_LIMIT = 20;
public static final String FILTER_CATEGORY = "category"; public static final String FILTER_CATEGORY = "category";

View File

@ -34,8 +34,6 @@ public class MiroChannel {
this.description = description; this.description = description;
this.items = items; this.items = items;
} }
@Override @Override
public String toString() { public String toString() {
@ -69,7 +67,9 @@ public class MiroChannel {
public ArrayList<MiroItem> getItems() { public ArrayList<MiroItem> getItems() {
return items; return items;
} }
public void setThumbnailUrl(String thumbnailUrl) {
this.thumbnailUrl = thumbnailUrl;
}
} }

View File

@ -6,8 +6,9 @@ import android.util.Log;
public class BitmapDecoder { public class BitmapDecoder {
private static final String TAG = "BitmapDecoder"; private static final String TAG = "BitmapDecoder";
private static int calculateSampleSize(int preferredLength, int width, int height) { private static int calculateSampleSize(int preferredLength, int width,
int height) {
int max = Math.max(width, height); int max = Math.max(width, height);
if (max < preferredLength) { if (max < preferredLength) {
return 1; return 1;
@ -27,18 +28,23 @@ public class BitmapDecoder {
} }
} }
} }
public static Bitmap decodeBitmap(int preferredLength, String fileUrl) { public static Bitmap decodeBitmap(int preferredLength, String fileUrl) {
BitmapFactory.Options options = new BitmapFactory.Options(); BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(fileUrl, options); BitmapFactory.decodeFile(fileUrl, options);
int sampleSize = calculateSampleSize(preferredLength, options.outWidth, int srcWidth = options.outWidth;
options.outHeight); int srcHeight = options.outHeight;
int sampleSize = calculateSampleSize(preferredLength, srcWidth,
srcHeight);
options.inJustDecodeBounds = false; options.inJustDecodeBounds = false;
options.inSampleSize = sampleSize; options.inSampleSize = sampleSize;
Bitmap decodedBitmap = BitmapFactory.decodeFile(fileUrl, options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options); options.inDither = false;
options.inScaled = false;
Bitmap decodedBitmap = BitmapFactory.decodeFile(fileUrl, options);
if (decodedBitmap == null) { if (decodedBitmap == null) {
Log.i(TAG, Log.i(TAG,
"Bitmap could not be decoded in custom sample size. Trying default sample size (path was " "Bitmap could not be decoded in custom sample size. Trying default sample size (path was "
@ -46,8 +52,12 @@ public class BitmapDecoder {
decodedBitmap = BitmapFactory.decodeFile(fileUrl); decodedBitmap = BitmapFactory.decodeFile(fileUrl);
} }
if (decodedBitmap != null) { if (decodedBitmap != null) {
return Bitmap.createScaledBitmap(decodedBitmap, if (preferredLength > srcWidth || preferredLength > srcHeight) {
preferredLength, preferredLength, false); return decodedBitmap;
} else {
return Bitmap.createScaledBitmap(decodedBitmap,
preferredLength, preferredLength, false);
}
} else { } else {
return null; return null;
} }