|
@@ -419,6 +419,7 @@ public class ThumbnailsCacheManager {
|
|
|
}
|
|
|
|
|
|
public static class MediaThumbnailGenerationTask extends AsyncTask<Object, Void, Bitmap> {
|
|
|
+ private enum Type {IMAGE, VIDEO}
|
|
|
private final WeakReference<ImageView> mImageViewReference;
|
|
|
private File mFile;
|
|
|
private String mImageKey = null;
|
|
@@ -440,9 +441,9 @@ public class ThumbnailsCacheManager {
|
|
|
}
|
|
|
|
|
|
if (MimeTypeUtil.isImage(mFile)) {
|
|
|
- thumbnail = doFileInBackground(mFile);
|
|
|
+ thumbnail = doFileInBackground(mFile, Type.IMAGE);
|
|
|
} else if (MimeTypeUtil.isVideo(mFile)) {
|
|
|
- thumbnail = doVideoInBackground(mFile);
|
|
|
+ thumbnail = doFileInBackground(mFile, Type.VIDEO);
|
|
|
}
|
|
|
}
|
|
|
} // the app should never break due to a problem with thumbnails
|
|
@@ -485,7 +486,7 @@ public class ThumbnailsCacheManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private Bitmap doFileInBackground(File file) {
|
|
|
+ private Bitmap doFileInBackground(File file, Type type) {
|
|
|
final String imageKey;
|
|
|
|
|
|
if (mImageKey != null) {
|
|
@@ -500,62 +501,46 @@ public class ThumbnailsCacheManager {
|
|
|
// Not found in disk cache
|
|
|
if (thumbnail == null) {
|
|
|
|
|
|
- int px = getThumbnailDimension();
|
|
|
-
|
|
|
- Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile(file.getAbsolutePath(), px, px);
|
|
|
-
|
|
|
- if (bitmap != null) {
|
|
|
- thumbnail = addThumbnailToCache(imageKey, bitmap, file.getPath(), px);
|
|
|
- }
|
|
|
- }
|
|
|
- return thumbnail;
|
|
|
- }
|
|
|
-
|
|
|
- private Bitmap doVideoInBackground(File file) {
|
|
|
- Bitmap bitmap;
|
|
|
- final String imageKey;
|
|
|
+ if (Type.IMAGE.equals(type)) {
|
|
|
+ int px = getThumbnailDimension();
|
|
|
|
|
|
- if (mImageKey != null) {
|
|
|
- imageKey = mImageKey;
|
|
|
- } else {
|
|
|
- imageKey = String.valueOf(file.hashCode());
|
|
|
- }
|
|
|
+ Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile(file.getAbsolutePath(), px, px);
|
|
|
|
|
|
- // Check disk cache in background thread
|
|
|
- bitmap = getBitmapFromDiskCache(imageKey);
|
|
|
-
|
|
|
- // Not found in disk cache
|
|
|
- if (bitmap == null) {
|
|
|
- MediaMetadataRetriever retriever = new MediaMetadataRetriever();
|
|
|
- try {
|
|
|
- retriever.setDataSource(file.getAbsolutePath());
|
|
|
- bitmap = retriever.getFrameAtTime(-1);
|
|
|
- } catch (Exception ex) {
|
|
|
- // can't create a bitmap
|
|
|
- } finally {
|
|
|
+ if (bitmap != null) {
|
|
|
+ thumbnail = addThumbnailToCache(imageKey, bitmap, file.getPath(), px);
|
|
|
+ }
|
|
|
+ } else if (Type.VIDEO.equals(type)) {
|
|
|
+ MediaMetadataRetriever retriever = new MediaMetadataRetriever();
|
|
|
try {
|
|
|
- retriever.release();
|
|
|
- } catch (RuntimeException ex) {
|
|
|
- // Ignore failure at this point.
|
|
|
+ retriever.setDataSource(file.getAbsolutePath());
|
|
|
+ thumbnail = retriever.getFrameAtTime(-1);
|
|
|
+ } catch (Exception ex) {
|
|
|
+ // can't create a bitmap
|
|
|
+ Log_OC.w(TAG, "Failed to create bitmap from video " + file.getAbsolutePath());
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ retriever.release();
|
|
|
+ } catch (RuntimeException ex) {
|
|
|
+ // Ignore failure at this point.
|
|
|
+ Log_OC.w(TAG, "Failed release MediaMetadataRetriever for " + file.getAbsolutePath());
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- if (bitmap == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
|
|
|
- // Scale down bitmap if too large.
|
|
|
- int px = getThumbnailDimension();
|
|
|
- int width = bitmap.getWidth();
|
|
|
- int height = bitmap.getHeight();
|
|
|
- int max = Math.max(width, height);
|
|
|
- if (max > px) {
|
|
|
- bitmap = BitmapUtils.scaleBitmap(bitmap, px, width, height, max);
|
|
|
- bitmap = addThumbnailToCache(imageKey, bitmap, file.getPath(), px);
|
|
|
+ if (thumbnail != null) {
|
|
|
+ // Scale down bitmap if too large.
|
|
|
+ int px = getThumbnailDimension();
|
|
|
+ int width = thumbnail.getWidth();
|
|
|
+ int height = thumbnail.getHeight();
|
|
|
+ int max = Math.max(width, height);
|
|
|
+ if (max > px) {
|
|
|
+ thumbnail = BitmapUtils.scaleBitmap(thumbnail, px, width, height, max);
|
|
|
+ thumbnail = addThumbnailToCache(imageKey, thumbnail, file.getPath(), px);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return bitmap;
|
|
|
+ return thumbnail;
|
|
|
}
|
|
|
}
|
|
|
|