Merge branch 'r3.9'
This commit is contained in:
commit
60e5763257
1 changed files with 40 additions and 18 deletions
|
@ -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;
|
||||||
|
|
||||||
|
@ -102,10 +101,11 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
|
||||||
private boolean handleSelectedImage(final Uri uri) {
|
private boolean handleSelectedImage(final Uri uri) {
|
||||||
boolean success = false;
|
boolean success = false;
|
||||||
InputStream inputStream = null;
|
InputStream inputStream = null;
|
||||||
|
int[] imageDimensions = null;
|
||||||
try {
|
try {
|
||||||
inputStream = app.getContentResolver().openInputStream(uri);
|
inputStream = app.getContentResolver().openInputStream(uri);
|
||||||
if (inputStream != null) {
|
if (inputStream != null) {
|
||||||
success = uploadImageToPlace(inputStream);
|
imageDimensions = calcImageDimensions(inputStream);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOG.error(e);
|
LOG.error(e);
|
||||||
|
@ -113,12 +113,27 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
|
||||||
} finally {
|
} finally {
|
||||||
Algorithms.closeStream(inputStream);
|
Algorithms.closeStream(inputStream);
|
||||||
}
|
}
|
||||||
|
if (imageDimensions != null && imageDimensions.length == 2) {
|
||||||
|
try {
|
||||||
|
inputStream = app.getContentResolver().openInputStream(uri);
|
||||||
|
if (inputStream != null) {
|
||||||
|
int width = imageDimensions[0];
|
||||||
|
int height = imageDimensions[1];
|
||||||
|
success = uploadImageToPlace(inputStream, width, height);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOG.error(e);
|
||||||
|
app.showToastMessage(R.string.cannot_upload_image);
|
||||||
|
} finally {
|
||||||
|
Algorithms.closeStream(inputStream);
|
||||||
|
}
|
||||||
|
}
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean uploadImageToPlace(InputStream image) {
|
private boolean uploadImageToPlace(InputStream image, int width, int height) {
|
||||||
boolean success = false;
|
boolean success = false;
|
||||||
InputStream serverData = new ByteArrayInputStream(compressImageToJpeg(image));
|
InputStream serverData = new ByteArrayInputStream(compressImageToJpeg(image, width, height));
|
||||||
String baseUrl = OPRConstants.getBaseUrl(app);
|
String baseUrl = OPRConstants.getBaseUrl(app);
|
||||||
// all these should be constant
|
// all these should be constant
|
||||||
String url = baseUrl + "api/ipfs/image";
|
String url = baseUrl + "api/ipfs/image";
|
||||||
|
@ -178,28 +193,35 @@ public class UploadPhotosAsyncTask extends AsyncTask<Void, Integer, Void> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] compressImageToJpeg(InputStream image) {
|
private int[] calcImageDimensions(InputStream image) {
|
||||||
BufferedInputStream bufferedInputStream = new BufferedInputStream(image);
|
BufferedInputStream bufferedInputStream = new BufferedInputStream(image);
|
||||||
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);
|
||||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
return new int[] { opts.outWidth, opts.outHeight };
|
||||||
int h = bmp.getHeight();
|
}
|
||||||
int w = bmp.getWidth();
|
|
||||||
|
private byte[] compressImageToJpeg(InputStream image, int width, int height) {
|
||||||
|
BufferedInputStream bufferedInputStream = new BufferedInputStream(image);
|
||||||
|
int w = width;
|
||||||
|
int h = height;
|
||||||
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();
|
BitmapFactory.Options 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;
|
|
||||||
}
|
}
|
||||||
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
||||||
bmp.compress(Bitmap.CompressFormat.JPEG, 90, os);
|
bmp.compress(Bitmap.CompressFormat.JPEG, 90, os);
|
||||||
return os.toByteArray();
|
return os.toByteArray();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue