diff options
| author | 2011-01-18 17:39:15 +0800 | |
|---|---|---|
| committer | 2011-01-18 18:25:32 +0800 | |
| commit | a9d0d47076ecf2d1739bb3534abc9deead8ebebd (patch) | |
| tree | e1c3290a850d079f59cb07e603acaeddd1d4a5d7 | |
| parent | 92cd4d0296cc8936fcccd97cdbc7a13b724efab4 (diff) | |
Change to stream decoding mode if the file descriptor cannot support seek.
bug: 3298498
Change-Id: Id7ae46bf8e885a417753edbd6648332052fee469
| -rw-r--r-- | core/jni/android/graphics/BitmapFactory.cpp | 10 | ||||
| -rw-r--r-- | graphics/java/android/graphics/BitmapFactory.java | 20 |
2 files changed, 26 insertions, 4 deletions
diff --git a/core/jni/android/graphics/BitmapFactory.cpp b/core/jni/android/graphics/BitmapFactory.cpp index dac748dcc028..864add9d92b2 100644 --- a/core/jni/android/graphics/BitmapFactory.cpp +++ b/core/jni/android/graphics/BitmapFactory.cpp @@ -510,6 +510,11 @@ static void nativeSetDefaultConfig(JNIEnv* env, jobject, int nativeConfig) { } } +static jboolean nativeIsSeekable(JNIEnv* env, jobject, jobject fileDescriptor) { + jint descriptor = env->GetIntField(fileDescriptor, gFileDescriptor_descriptor); + return ::lseek64(descriptor, 0, SEEK_CUR) != -1 ? JNI_TRUE : JNI_FALSE; +} + /////////////////////////////////////////////////////////////////////////////// static JNINativeMethod gMethods[] = { @@ -539,6 +544,11 @@ static JNINativeMethod gMethods[] = { }, { "nativeSetDefaultConfig", "(I)V", (void*)nativeSetDefaultConfig }, + + { "nativeIsSeekable", + "(Ljava/io/FileDescriptor;)Z", + (void*)nativeIsSeekable + }, }; static JNINativeMethod gOptionsMethods[] = { diff --git a/graphics/java/android/graphics/BitmapFactory.java b/graphics/java/android/graphics/BitmapFactory.java index 8309f7abe169..3f76a1012426 100644 --- a/graphics/java/android/graphics/BitmapFactory.java +++ b/graphics/java/android/graphics/BitmapFactory.java @@ -556,11 +556,22 @@ public class BitmapFactory { * @return the decoded bitmap, or null */ public static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, Options opts) { - Bitmap bm = nativeDecodeFileDescriptor(fd, outPadding, opts); - if (bm == null && opts != null && opts.inBitmap != null) { - throw new IllegalArgumentException("Problem decoding into existing bitmap"); + if (nativeIsSeekable(fd)) { + Bitmap bm = nativeDecodeFileDescriptor(fd, outPadding, opts); + if (bm == null && opts != null && opts.inBitmap != null) { + throw new IllegalArgumentException("Problem decoding into existing bitmap"); + } + return finishDecode(bm, outPadding, opts); + } else { + FileInputStream fis = new FileInputStream(fd); + try { + return decodeStream(fis, outPadding, opts); + } finally { + try { + fis.close(); + } catch (Throwable t) {/* ignore */} + } } - return finishDecode(bm, outPadding, opts); } /** @@ -607,4 +618,5 @@ public class BitmapFactory { private static native Bitmap nativeDecodeByteArray(byte[] data, int offset, int length, Options opts); private static native byte[] nativeScaleNinePatch(byte[] chunk, float scale, Rect pad); + private static native boolean nativeIsSeekable(FileDescriptor fd); } |