mirror of
https://github.com/oxen-io/session-android.git
synced 2024-12-25 01:07:47 +00:00
Merge pull request #1892 from mcginty/bitmap-memory
fallback to imprecise scaling if low memory
This commit is contained in:
commit
2045c828be
@ -11,12 +11,12 @@ import android.graphics.Rect;
|
|||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
public class BitmapUtil {
|
public class BitmapUtil {
|
||||||
|
private static final String TAG = BitmapUtil.class.getSimpleName();
|
||||||
|
|
||||||
private static final int MAX_COMPRESSION_QUALITY = 95;
|
private static final int MAX_COMPRESSION_QUALITY = 95;
|
||||||
private static final int MIN_COMPRESSION_QUALITY = 50;
|
private static final int MIN_COMPRESSION_QUALITY = 50;
|
||||||
@ -26,9 +26,17 @@ public class BitmapUtil {
|
|||||||
int maxHeight, int maxSize)
|
int maxHeight, int maxSize)
|
||||||
throws IOException, BitmapDecodingException
|
throws IOException, BitmapDecodingException
|
||||||
{
|
{
|
||||||
InputStream measure = context.getContentResolver().openInputStream(uri);
|
Bitmap bitmap;
|
||||||
InputStream data = context.getContentResolver().openInputStream(uri);
|
try {
|
||||||
Bitmap bitmap = createScaledBitmap(measure, data, maxWidth, maxHeight);
|
bitmap = createScaledBitmap(context.getContentResolver().openInputStream(uri),
|
||||||
|
context.getContentResolver().openInputStream(uri),
|
||||||
|
maxWidth, maxHeight, false);
|
||||||
|
} catch(OutOfMemoryError oome) {
|
||||||
|
Log.w(TAG, "OutOfMemoryError when scaling precisely, doing rough scale to save memory instead");
|
||||||
|
bitmap = createScaledBitmap(context.getContentResolver().openInputStream(uri),
|
||||||
|
context.getContentResolver().openInputStream(uri),
|
||||||
|
maxWidth, maxHeight, true);
|
||||||
|
}
|
||||||
int quality = MAX_COMPRESSION_QUALITY;
|
int quality = MAX_COMPRESSION_QUALITY;
|
||||||
int attempts = 0;
|
int attempts = 0;
|
||||||
|
|
||||||
@ -53,39 +61,56 @@ public class BitmapUtil {
|
|||||||
final BitmapFactory.Options options = getImageDimensions(measure);
|
final BitmapFactory.Options options = getImageDimensions(measure);
|
||||||
final int outWidth = (int)(options.outWidth * scale);
|
final int outWidth = (int)(options.outWidth * scale);
|
||||||
final int outHeight = (int)(options.outHeight * scale);
|
final int outHeight = (int)(options.outHeight * scale);
|
||||||
Log.w("BitmapUtil", "creating scaled bitmap with scale " + scale + " => " + outWidth + "x" + outHeight);
|
Log.w(TAG, "creating scaled bitmap with scale " + scale + " => " + outWidth + "x" + outHeight);
|
||||||
return createScaledBitmap(data, outWidth, outHeight, options);
|
return createScaledBitmap(data, outWidth, outHeight, options, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Bitmap createScaledBitmap(InputStream measure, InputStream data,
|
public static Bitmap createScaledBitmap(InputStream measure, InputStream data,
|
||||||
int maxWidth, int maxHeight)
|
int maxWidth, int maxHeight)
|
||||||
throws BitmapDecodingException
|
throws BitmapDecodingException
|
||||||
{
|
{
|
||||||
final BitmapFactory.Options options = getImageDimensions(measure);
|
return createScaledBitmap(measure, data, maxWidth, maxHeight, false);
|
||||||
return createScaledBitmap(data, maxWidth, maxHeight, options);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Bitmap createScaledBitmap(InputStream data,
|
public static Bitmap createScaledBitmap(InputStream measure, InputStream data,
|
||||||
int maxWidth, int maxHeight, BitmapFactory.Options options)
|
int maxWidth, int maxHeight, boolean constrainedMemory)
|
||||||
|
throws BitmapDecodingException
|
||||||
|
{
|
||||||
|
final BitmapFactory.Options options = getImageDimensions(measure);
|
||||||
|
return createScaledBitmap(data, maxWidth, maxHeight, options, constrainedMemory);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Bitmap createScaledBitmap(InputStream data, int maxWidth, int maxHeight,
|
||||||
|
BitmapFactory.Options options, boolean constrainedMemory)
|
||||||
throws BitmapDecodingException
|
throws BitmapDecodingException
|
||||||
{
|
{
|
||||||
final int imageWidth = options.outWidth;
|
final int imageWidth = options.outWidth;
|
||||||
final int imageHeight = options.outHeight;
|
final int imageHeight = options.outHeight;
|
||||||
|
|
||||||
int scaler = 1;
|
int scaler = 1;
|
||||||
|
int scaleFactor = (constrainedMemory ? 1 : 2);
|
||||||
while ((imageWidth / scaler / 2 >= maxWidth) && (imageHeight / scaler / 2 >= maxHeight))
|
while ((imageWidth / scaler / scaleFactor >= maxWidth) && (imageHeight / scaler / scaleFactor >= maxHeight)) {
|
||||||
scaler *= 2;
|
scaler *= 2;
|
||||||
|
}
|
||||||
|
|
||||||
options.inSampleSize = scaler;
|
options.inSampleSize = scaler;
|
||||||
options.inJustDecodeBounds = false;
|
options.inJustDecodeBounds = false;
|
||||||
|
|
||||||
Bitmap roughThumbnail = BitmapFactory.decodeStream(new BufferedInputStream(data), null, options);
|
FlushedInputStream is = new FlushedInputStream(data);
|
||||||
Log.w("BitmapUtil", "rough scale " + (imageWidth) + "x" + (imageHeight) +
|
Bitmap roughThumbnail = BitmapFactory.decodeStream(is, null, options);
|
||||||
|
try {
|
||||||
|
is.close();
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
Log.w(TAG, "IOException thrown when closing an images InputStream", ioe);
|
||||||
|
}
|
||||||
|
Log.w(TAG, "rough scale " + (imageWidth) + "x" + (imageHeight) +
|
||||||
" => " + (options.outWidth) + "x" + (options.outHeight));
|
" => " + (options.outWidth) + "x" + (options.outHeight));
|
||||||
if (roughThumbnail == null) {
|
if (roughThumbnail == null) {
|
||||||
throw new BitmapDecodingException("Decoded stream was null.");
|
throw new BitmapDecodingException("Decoded stream was null.");
|
||||||
}
|
}
|
||||||
|
if (constrainedMemory) {
|
||||||
|
return roughThumbnail;
|
||||||
|
}
|
||||||
|
|
||||||
if (options.outWidth > maxWidth || options.outHeight > maxHeight) {
|
if (options.outWidth > maxWidth || options.outHeight > maxHeight) {
|
||||||
final float aspectWidth, aspectHeight;
|
final float aspectWidth, aspectHeight;
|
||||||
@ -101,10 +126,14 @@ public class BitmapUtil {
|
|||||||
aspectWidth = (aspectHeight / options.outHeight) * options.outWidth;
|
aspectWidth = (aspectHeight / options.outHeight) * options.outWidth;
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.w("BitmapUtil", "fine scale " + options.outWidth + "x" + options.outHeight +
|
Log.w(TAG, "fine scale " + options.outWidth + "x" + options.outHeight +
|
||||||
" => " + aspectWidth + "x" + aspectHeight);
|
" => " + aspectWidth + "x" + aspectHeight);
|
||||||
Bitmap scaledThumbnail = Bitmap.createScaledBitmap(roughThumbnail, (int)aspectWidth, (int)aspectHeight, true);
|
Bitmap scaledThumbnail = null;
|
||||||
|
try {
|
||||||
|
scaledThumbnail = Bitmap.createScaledBitmap(roughThumbnail, (int) aspectWidth, (int) aspectHeight, true);
|
||||||
|
} finally {
|
||||||
if (roughThumbnail != scaledThumbnail) roughThumbnail.recycle();
|
if (roughThumbnail != scaledThumbnail) roughThumbnail.recycle();
|
||||||
|
}
|
||||||
return scaledThumbnail;
|
return scaledThumbnail;
|
||||||
} else {
|
} else {
|
||||||
return roughThumbnail;
|
return roughThumbnail;
|
||||||
@ -114,8 +143,13 @@ public class BitmapUtil {
|
|||||||
private static BitmapFactory.Options getImageDimensions(InputStream inputStream) {
|
private static BitmapFactory.Options getImageDimensions(InputStream inputStream) {
|
||||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||||
options.inJustDecodeBounds = true;
|
options.inJustDecodeBounds = true;
|
||||||
BitmapFactory.decodeStream(inputStream, null, options);
|
FlushedInputStream fis = new FlushedInputStream(inputStream);
|
||||||
|
BitmapFactory.decodeStream(fis, null, options);
|
||||||
|
try {
|
||||||
|
fis.close();
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
Log.w(TAG, "failed to close the InputStream after reading image dimensions");
|
||||||
|
}
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
33
src/org/thoughtcrime/securesms/util/FlushedInputStream.java
Normal file
33
src/org/thoughtcrime/securesms/util/FlushedInputStream.java
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package org.thoughtcrime.securesms.util;
|
||||||
|
|
||||||
|
import java.io.FilterInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Memory-friendly workaround found from https://code.google.com/p/android/issues/detail?id=6066#c23
|
||||||
|
* to solve decoding problems in bitmaps from InputStreams that don't skip if no more stream is available.
|
||||||
|
*/
|
||||||
|
class FlushedInputStream extends FilterInputStream {
|
||||||
|
public FlushedInputStream(InputStream inputStream) {
|
||||||
|
super(inputStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long skip(long n) throws IOException {
|
||||||
|
long totalBytesSkipped = 0L;
|
||||||
|
while (totalBytesSkipped < n) {
|
||||||
|
long bytesSkipped = in.skip(n - totalBytesSkipped);
|
||||||
|
if (bytesSkipped == 0L) {
|
||||||
|
int inByte = read();
|
||||||
|
if (inByte < 0) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
bytesSkipped = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
totalBytesSkipped += bytesSkipped;
|
||||||
|
}
|
||||||
|
return totalBytesSkipped;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user