Prevents an out-of-memory crash when uploading images and also downsizing now uses significantly less memory.

This commit is contained in:
Vavassor 2017-03-31 17:53:35 -04:00
parent 0f5130f692
commit 26c722c950
2 changed files with 75 additions and 35 deletions

View File

@ -945,16 +945,9 @@ public class ComposeActivity extends BaseActivity {
private void downsizeMedia(final QueuedMedia item) { private void downsizeMedia(final QueuedMedia item) {
item.readyStage = QueuedMedia.ReadyStage.DOWNSIZING; item.readyStage = QueuedMedia.ReadyStage.DOWNSIZING;
InputStream stream;
try { new DownsizeImageTask(STATUS_MEDIA_SIZE_LIMIT, getContentResolver(),
stream = getContentResolver().openInputStream(item.uri); new DownsizeImageTask.Listener() {
} catch (FileNotFoundException e) {
onMediaDownsizeFailure(item);
return;
}
Bitmap bitmap = BitmapFactory.decodeStream(stream);
IOUtils.closeQuietly(stream);
new DownsizeImageTask(STATUS_MEDIA_SIZE_LIMIT, new DownsizeImageTask.Listener() {
@Override @Override
public void onSuccess(List<byte[]> contentList) { public void onSuccess(List<byte[]> contentList) {
item.content = contentList.get(0); item.content = contentList.get(0);
@ -965,7 +958,7 @@ public class ComposeActivity extends BaseActivity {
public void onFailure() { public void onFailure() {
onMediaDownsizeFailure(item); onMediaDownsizeFailure(item);
} }
}).execute(bitmap); }).execute(item.uri);
} }
private void onMediaDownsizeFailure(QueuedMedia item) { private void onMediaDownsizeFailure(QueuedMedia item) {

View File

@ -15,35 +15,63 @@
package com.keylesspalace.tusky; package com.keylesspalace.tusky;
import android.content.ContentResolver;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
class DownsizeImageTask extends AsyncTask<Bitmap, Void, Boolean> { class DownsizeImageTask extends AsyncTask<Uri, Void, Boolean> {
private Listener listener;
private int sizeLimit; private int sizeLimit;
private ContentResolver contentResolver;
private Listener listener;
private List<byte[]> resultList; private List<byte[]> resultList;
DownsizeImageTask(int sizeLimit, Listener listener) { DownsizeImageTask(int sizeLimit, ContentResolver contentResolver, Listener listener) {
this.listener = listener;
this.sizeLimit = sizeLimit; this.sizeLimit = sizeLimit;
this.contentResolver = contentResolver;
this.listener = listener;
} }
private static Bitmap scaleDown(Bitmap source, float maxImageSize, boolean filter) { private static int calculateInSampleSize(int width, int height, int requiredScale) {
float ratio = Math.min(maxImageSize / source.getWidth(), maxImageSize / source.getHeight()); int inSampleSize = 1;
int width = Math.round(ratio * source.getWidth()); if (height > requiredScale || width > requiredScale) {
int height = Math.round(ratio * source.getHeight()); final int halfHeight = height / 2;
return Bitmap.createScaledBitmap(source, width, height, filter); final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while (halfHeight / inSampleSize >= requiredScale
&& halfWidth / inSampleSize >= requiredScale) {
inSampleSize *= 2;
}
}
return inSampleSize;
} }
@Override @Override
protected Boolean doInBackground(Bitmap... bitmaps) { protected Boolean doInBackground(Uri... uris) {
final int count = bitmaps.length; resultList = new ArrayList<>();
resultList = new ArrayList<>(count); for (Uri uri : uris) {
for (Bitmap bitmap : bitmaps) { InputStream inputStream;
try {
inputStream = contentResolver.openInputStream(uri);
} catch (FileNotFoundException e) {
return false;
}
// Initially, just get the image dimensions.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, options);
int beforeWidth = options.outWidth;
int beforeHeight = options.outHeight;
IOUtils.closeQuietly(inputStream);
// Then use that information to determine how much to compress.
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
/* Unfortunately, there isn't a determined worst case compression ratio for image /* Unfortunately, there isn't a determined worst case compression ratio for image
* formats. So, the only way to tell if they're too big is to compress them and * formats. So, the only way to tell if they're too big is to compress them and
@ -54,7 +82,25 @@ class DownsizeImageTask extends AsyncTask<Bitmap, Void, Boolean> {
int scaledImageSize = 4096; int scaledImageSize = 4096;
do { do {
stream.reset(); stream.reset();
Bitmap scaledBitmap = scaleDown(bitmap, scaledImageSize, true); try {
inputStream = contentResolver.openInputStream(uri);
} catch (FileNotFoundException e) {
return false;
}
options.inSampleSize = calculateInSampleSize(beforeWidth, beforeHeight,
scaledImageSize);
options.inJustDecodeBounds = false;
Bitmap scaledBitmap;
try {
scaledBitmap = BitmapFactory.decodeStream(inputStream, null, options);
} catch (OutOfMemoryError error) {
return false;
} finally {
IOUtils.closeQuietly(inputStream);
}
if (scaledBitmap == null) {
return false;
}
Bitmap.CompressFormat format; Bitmap.CompressFormat format;
/* It's not likely the user will give transparent images over the upload limit, but /* It's not likely the user will give transparent images over the upload limit, but
* if they do, make sure the transparency is retained. */ * if they do, make sure the transparency is retained. */
@ -64,6 +110,7 @@ class DownsizeImageTask extends AsyncTask<Bitmap, Void, Boolean> {
format = Bitmap.CompressFormat.PNG; format = Bitmap.CompressFormat.PNG;
} }
scaledBitmap.compress(format, 75, stream); scaledBitmap.compress(format, 75, stream);
scaledBitmap.recycle();
scaledImageSize /= 2; scaledImageSize /= 2;
iterations++; iterations++;
} while (stream.size() > sizeLimit); } while (stream.size() > sizeLimit);
@ -86,7 +133,7 @@ class DownsizeImageTask extends AsyncTask<Bitmap, Void, Boolean> {
super.onPostExecute(successful); super.onPostExecute(successful);
} }
public interface Listener { interface Listener {
void onSuccess(List<byte[]> contentList); void onSuccess(List<byte[]> contentList);
void onFailure(); void onFailure();
} }