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
android:id="@+id/imgvChannelimage"
android:src="@drawable/default_cover"
android:layout_width="55dip"
android:layout_height="55dip"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="1dip"
android:layout_marginRight="4dip"
android:cropToPadding="true" />
android:src="@drawable/default_cover" />
<LinearLayout
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.Snapshot;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.feed.FeedImage;
@ -159,6 +160,7 @@ public class FeedImageLoader {
.getThumbnailUrl());
if (bitmap == null) {
boolean isInDiskCache = false;
/*
try {
isInDiskCache = isInThumbnailDiskCache(channel
.getThumbnailUrl());
@ -166,10 +168,13 @@ public class FeedImageLoader {
e.printStackTrace();
Log.e(TAG, "Error when trying to read disk cache");
}
*/
if (isInDiskCache) {
executor.submit(new MiroGuideDiskCacheLoader(handler,
target, channel, LENGTH_BASE_THUMBNAIL));
} else {
if (AppConfig.DEBUG) Log.d(TAG, "Starting new thumbnail download");
executor.submit(new MiroGuideThumbnailDownloader(handler,
target, channel, LENGTH_BASE_THUMBNAIL));
}

View File

@ -1,6 +1,7 @@
package de.danoeh.antennapod.asynctask;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@ -9,6 +10,7 @@ import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import com.jakewharton.DiskLruCache;
import com.jakewharton.DiskLruCache.Editor;
@ -19,6 +21,7 @@ import de.danoeh.antennapod.miroguide.model.MiroChannel;
import de.danoeh.antennapod.util.BitmapDecoder;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;
@ -39,7 +42,7 @@ public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
@Override
protected void onPostExecute() {
if (exception != null) {
if (exception == null) {
super.onPostExecute();
} else {
Log.e(TAG, "Failed to download thumbnail");
@ -58,40 +61,43 @@ public class MiroGuideThumbnailDownloader extends BitmapDecodeWorkerTask {
File destination = new File(PodcastApp.getInstance().getCacheDir(),
Integer.toString(fileUrl.hashCode()));
try {
DiskLruCache diskCache = FeedImageLoader.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);
/*
* DiskLruCache diskCache =
* FeedImageLoader.openThubmnailDiskCache(); Editor editor =
* diskCache.edit(fileUrl);
*/
if (AppConfig.DEBUG) Log.d(TAG, "Downloading " + fileUrl);
URLConnection connection = url.openConnection();
connection.connect();
byte inputBuffer[] = new byte[1024];
BufferedInputStream 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();
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 {
int count = 0;
while ((count = input.read(inputBuffer)) != -1) {
output.write(inputBuffer, 0, count);
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) {
e.printStackTrace();
miroChannel.setThumbnailUrl(null);
endBackgroundTask();
} finally {
if (destination.exists()) {

View File

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

View File

@ -19,7 +19,7 @@ import android.net.http.AndroidHttpClient;
/** Executes HTTP requests and returns the results. */
public class MiroGuideConnector {
private AndroidHttpClient httpClient;
private HttpClient httpClient;
private static final String HOST_URL = "https://www.miroguide.com/api/";
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";
public MiroGuideConnector() {
httpClient = AndroidHttpClient.newInstance(null);
httpClient = new DefaultHttpClient();
}
public void shutdown() {
httpClient.close();
httpClient.getConnectionManager().shutdown();
}
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. */
public class MiroGuideService {
private static final String TAG = "MiroGuideService";
public static final int DEFAULT_CHANNEL_LIMIT = 20;
public static final String FILTER_CATEGORY = "category";

View File

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

View File

@ -6,8 +6,9 @@ import android.util.Log;
public class 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);
if (max < preferredLength) {
return 1;
@ -27,18 +28,23 @@ public class BitmapDecoder {
}
}
}
public static Bitmap decodeBitmap(int preferredLength, String fileUrl) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(fileUrl, options);
int sampleSize = calculateSampleSize(preferredLength, options.outWidth,
options.outHeight);
int srcWidth = options.outWidth;
int srcHeight = options.outHeight;
int sampleSize = calculateSampleSize(preferredLength, srcWidth,
srcHeight);
options.inJustDecodeBounds = false;
options.inSampleSize = sampleSize;
Bitmap decodedBitmap = BitmapFactory.decodeFile(fileUrl,
options);
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inDither = false;
options.inScaled = false;
Bitmap decodedBitmap = BitmapFactory.decodeFile(fileUrl, options);
if (decodedBitmap == null) {
Log.i(TAG,
"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);
}
if (decodedBitmap != null) {
return Bitmap.createScaledBitmap(decodedBitmap,
preferredLength, preferredLength, false);
if (preferredLength > srcWidth || preferredLength > srcHeight) {
return decodedBitmap;
} else {
return Bitmap.createScaledBitmap(decodedBitmap,
preferredLength, preferredLength, false);
}
} else {
return null;
}