summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorJeff Sharkey <jsharkey@android.com>2013-08-23 23:18:11 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2013-08-23 23:18:11 +0000
commit365768fd3533343d6631875d7d46882907f7ab09 (patch)
tree2229069f7e5fdcd8006a97c4664e8466f82bda3e /core
parent98d6212327d1c483894b642d5e24c3aac9fca9cf (diff)
parent6398343e83b3fd11dd6536cf6f390a52c1e19d2e (diff)
downloadframeworks_base-365768fd3533343d6631875d7d46882907f7ab09.zip
frameworks_base-365768fd3533343d6631875d7d46882907f7ab09.tar.gz
frameworks_base-365768fd3533343d6631875d7d46882907f7ab09.tar.bz2
Merge "Return EXIF thumbnails when available." into klp-dev
Diffstat (limited to 'core')
-rw-r--r--core/java/android/provider/DocumentsContract.java56
1 files changed, 45 insertions, 11 deletions
diff --git a/core/java/android/provider/DocumentsContract.java b/core/java/android/provider/DocumentsContract.java
index 0d1a740..65c9220 100644
--- a/core/java/android/provider/DocumentsContract.java
+++ b/core/java/android/provider/DocumentsContract.java
@@ -16,6 +16,9 @@
package android.provider;
+import static android.net.TrafficStats.KB_IN_BYTES;
+import static libcore.io.OsConstants.SEEK_SET;
+
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
@@ -36,7 +39,10 @@ import android.util.Log;
import com.google.android.collect.Lists;
+import libcore.io.ErrnoException;
+import libcore.io.IoBridge;
import libcore.io.IoUtils;
+import libcore.io.Libcore;
import java.io.FileDescriptor;
import java.io.IOException;
@@ -527,25 +533,53 @@ public final class DocumentsContract {
* @return decoded thumbnail, or {@code null} if problem was encountered.
*/
public static Bitmap getThumbnail(ContentResolver resolver, Uri documentUri, Point size) {
- final Bundle opts = new Bundle();
- opts.putParcelable(EXTRA_THUMBNAIL_SIZE, size);
+ final Bundle openOpts = new Bundle();
+ openOpts.putParcelable(DocumentsContract.EXTRA_THUMBNAIL_SIZE, size);
AssetFileDescriptor afd = null;
try {
- afd = resolver.openTypedAssetFileDescriptor(documentUri, "image/*", opts);
+ afd = resolver.openTypedAssetFileDescriptor(documentUri, "image/*", openOpts);
final FileDescriptor fd = afd.getFileDescriptor();
- final BitmapFactory.Options bitmapOpts = new BitmapFactory.Options();
+ final long offset = afd.getStartOffset();
+ final long length = afd.getDeclaredLength();
+
+ // Some thumbnails might be a region inside a larger file, such as
+ // an EXIF thumbnail. Since BitmapFactory aggressively seeks around
+ // the entire file, we read the region manually.
+ byte[] region = null;
+ if (offset > 0 && length <= 64 * KB_IN_BYTES) {
+ region = new byte[(int) length];
+ Libcore.os.lseek(fd, offset, SEEK_SET);
+ if (IoBridge.read(fd, region, 0, region.length) != region.length) {
+ region = null;
+ }
+ }
- bitmapOpts.inJustDecodeBounds = true;
- BitmapFactory.decodeFileDescriptor(fd, null, bitmapOpts);
+ // We requested a rough thumbnail size, but the remote size may have
+ // returned something giant, so defensively scale down as needed.
+ final BitmapFactory.Options opts = new BitmapFactory.Options();
+ opts.inJustDecodeBounds = true;
+ if (region != null) {
+ BitmapFactory.decodeByteArray(region, 0, region.length, opts);
+ } else {
+ BitmapFactory.decodeFileDescriptor(fd, null, opts);
+ }
- final int widthSample = bitmapOpts.outWidth / size.x;
- final int heightSample = bitmapOpts.outHeight / size.y;
+ final int widthSample = opts.outWidth / size.x;
+ final int heightSample = opts.outHeight / size.y;
- bitmapOpts.inJustDecodeBounds = false;
- bitmapOpts.inSampleSize = Math.min(widthSample, heightSample);
- return BitmapFactory.decodeFileDescriptor(fd, null, bitmapOpts);
+ opts.inJustDecodeBounds = false;
+ opts.inSampleSize = Math.min(widthSample, heightSample);
+ Log.d(TAG, "Decoding with sample size " + opts.inSampleSize);
+ if (region != null) {
+ return BitmapFactory.decodeByteArray(region, 0, region.length, opts);
+ } else {
+ return BitmapFactory.decodeFileDescriptor(fd, null, opts);
+ }
+ } catch (ErrnoException e) {
+ Log.w(TAG, "Failed to load thumbnail for " + documentUri + ": " + e);
+ return null;
} catch (IOException e) {
Log.w(TAG, "Failed to load thumbnail for " + documentUri + ": " + e);
return null;