Fix OutOfMemory while uploading image to OPR

This commit is contained in:
max-klaus 2021-02-03 21:17:18 +03:00
parent 1d2d5350ab
commit 894a36fed8

View file

@ -4,7 +4,6 @@ import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener; import android.content.DialogInterface.OnDismissListener;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
@ -29,6 +28,7 @@ import org.openplacereviews.opendb.util.exception.FailedVerificationException;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
@ -116,7 +116,7 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
return success; return success;
} }
private boolean uploadImageToPlace(InputStream image) { private boolean uploadImageToPlace(InputStream image) throws IOException {
boolean success = false; boolean success = false;
InputStream serverData = new ByteArrayInputStream(compressImageToJpeg(image)); InputStream serverData = new ByteArrayInputStream(compressImageToJpeg(image));
String baseUrl = OPRConstants.getBaseUrl(app); String baseUrl = OPRConstants.getBaseUrl(app);
@ -178,27 +178,31 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
} }
} }
private byte[] compressImageToJpeg(InputStream image) { private byte[] compressImageToJpeg(InputStream image) throws IOException {
BufferedInputStream bufferedInputStream = new BufferedInputStream(image); BufferedInputStream bufferedInputStream = new BufferedInputStream(image);
bufferedInputStream.mark(1024 * 200);
BitmapFactory.Options opts = new BitmapFactory.Options(); BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 4; opts.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeStream(bufferedInputStream, null, opts); BitmapFactory.decodeStream(bufferedInputStream, null, opts);
int w = opts.outWidth;
int h = opts.outHeight;
bufferedInputStream.reset();
ByteArrayOutputStream os = new ByteArrayOutputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream();
int h = bmp.getHeight();
int w = bmp.getWidth();
boolean scale = false; boolean scale = false;
int divider = 1;
while (w > MAX_IMAGE_LENGTH || h > MAX_IMAGE_LENGTH) { while (w > MAX_IMAGE_LENGTH || h > MAX_IMAGE_LENGTH) {
w = w / 2; w /= 2;
h = h / 2; h /= 2;
divider *= 2;
scale = true; scale = true;
} }
Bitmap bmp;
if (scale) { if (scale) {
Matrix matrix = new Matrix(); opts = new BitmapFactory.Options();
matrix.postScale(w, h); opts.inSampleSize = divider;
Bitmap resizedBitmap = Bitmap.createBitmap( bmp = BitmapFactory.decodeStream(bufferedInputStream, null, opts);
bmp, 0, 0, w, h, matrix, false); } else {
bmp.recycle(); bmp = BitmapFactory.decodeStream(bufferedInputStream);
bmp = resizedBitmap;
} }
bmp.compress(Bitmap.CompressFormat.JPEG, 90, os); bmp.compress(Bitmap.CompressFormat.JPEG, 90, os);
return os.toByteArray(); return os.toByteArray();