Commit c99fefb8 authored by Evan W. Patton's avatar Evan W. Patton Committed by Jeffrey Schiller

Fix image loading in getBitmapDrawableAsync

On phones with smaller memory footprints, the use of InputStream.mark
was causing an IOException to be thrown that resulted in images not
loading. This patch loads the entirety of the image into a byte[] and
then uses that in conjunction with a ByteArrayInputStream to pass the
image around to the multiple interfaces that need it without reloading
the content. This fixes the failure to load many images in the
emulator.

Change-Id: If7185e8e1a02e19a46037a4dd4df545241fa7f2b
parent 08ad4ee1
......@@ -25,7 +25,8 @@ import android.view.Display;
import android.view.WindowManager;
import android.widget.VideoView;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
......@@ -391,14 +392,55 @@ public class MediaUtil {
// When the app says to fetch the image, we need to get the latest image, not one that we
// cached previously.
BufferedInputStream is = null;
Log.d(LOG_TAG, "mediaPath = " + mediaPath);
InputStream is = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
int read;
try {
is = new BufferedInputStream(openMedia(form, mediaPath, mediaSource));
is.mark(8*1024*1024);
BitmapFactory.Options options = getBitmapOptions(form, is, mediaPath);
is.reset();
Log.d(LOG_TAG, "mediaPath = " + mediaPath);
BitmapDrawable originalBitmapDrawable = new BitmapDrawable(form.getResources(), decodeStream(is, null, options));
// copy the input stream to an in-memory buffer
is = openMedia(form, mediaPath, mediaSource);
while((read = is.read(buf)) > 0) {
bos.write(buf, 0, read);
}
buf = bos.toByteArray();
} catch(IOException e) {
if (mediaSource == MediaSource.CONTACT_URI) {
// There's no photo for this contact, return a placeholder image.
BitmapDrawable drawable = new BitmapDrawable(form.getResources(),
BitmapFactory.decodeResource(form.getResources(),
android.R.drawable.picture_frame, null));
continuation.onSuccess(drawable);
return;
}
Log.d(LOG_TAG, "IOException reading file.", e);
continuation.onFailure(e.getMessage());
return;
} finally {
if (is != null) {
try {
is.close();
} catch(IOException e) {
// suppress error on close
Log.w(LOG_TAG, "Unexpected error on close", e);
}
}
is = null;
try {
bos.close();
} catch(IOException e) {
// Should never fail to close a ByteArrayOutputStream
}
bos = null;
}
ByteArrayInputStream bis = new ByteArrayInputStream(buf);
read = buf.length;
buf = null;
try {
bis.mark(read);
BitmapFactory.Options options = getBitmapOptions(form, bis, mediaPath);
bis.reset();
BitmapDrawable originalBitmapDrawable = new BitmapDrawable(form.getResources(), decodeStream(bis, null, options));
// If options.inSampleSize == 1, then the image was not unreasonably large and may represent
// the actual size the user intended for the image. However we still have to scale it by
// the device density.
......@@ -429,21 +471,13 @@ public class MediaUtil {
originalBitmapDrawable = null; // So it will get GC'd on the next line
System.gc(); // We likely used a lot of memory, so gc now.
continuation.onSuccess(scaledBitmapDrawable);
} catch (IOException e) {
if (mediaSource == MediaSource.CONTACT_URI) {
// There's no photo for this contact, return a placeholder image.
BitmapDrawable drawable = new BitmapDrawable(form.getResources(),
BitmapFactory.decodeResource(form.getResources(),
android.R.drawable.picture_frame, null));
continuation.onSuccess(drawable);
}
continuation.onFailure(e.getMessage());
} catch(Exception e) {
Log.w(LOG_TAG, "Exception while loading media.", e);
continuation.onFailure(e.getMessage());
} finally {
if (is != null) {
if (bis != null) {
try {
is.close();
bis.close();
} catch(IOException e) {
// suppress error on close
Log.w(LOG_TAG, "Unexpected error on close", e);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment