Removed image size limit by replacing BitmapFactory.decodeStream with BitmapFactory.decodeByteArray, simplified the code a bit

pull/15/head
Olga Miller 2023-03-04 17:26:19 +01:00
rodzic 8ce13b9fc5
commit c6be28df56
2 zmienionych plików z 48 dodań i 46 usunięć

Wyświetl plik

@ -182,7 +182,7 @@ public class CropView extends AppCompatImageView {
invalidate(); invalidate();
} }
public void setBitmap(@NonNull InputStream stream) throws IOException, IllegalArgumentException { public void setBitmap(@NonNull InputStream stream) throws Exception {
mImageOK = false; mImageOK = false;
mOrientation = 0; mOrientation = 0;
recycle(); recycle();
@ -190,35 +190,48 @@ public class CropView extends AppCompatImageView {
invalidate(); invalidate();
} }
private void loadImage(InputStream stream) throws IOException, IllegalArgumentException { private void loadImage(InputStream stream) throws Exception {
// app6 + exif
int bufferBytes = 1048576;
if (!stream.markSupported())
stream = new BufferedInputStream(stream, bufferBytes);
stream.mark(bufferBytes);
BitmapFactory.Options options = new BitmapFactory.Options(); BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new BufferedInputStream(stream), null, options); byte[] streamBytes = null;
stream.reset(); String errorMessage = null;
try {
int length = stream.available();
if (length > 0) {
streamBytes = new byte[length];
if (length == stream.read(streamBytes, 0, streamBytes.length)) {
BitmapFactory.decodeByteArray(streamBytes, 0, streamBytes.length, options);
}
else
streamBytes = null;
}
} catch (Exception ex) {
errorMessage = ex.getMessage();
streamBytes = null;
}
mImageWidth = options.outWidth; mImageWidth = options.outWidth;
mImageHeight = options.outHeight; mImageHeight = options.outHeight;
if (mImageWidth * mImageHeight < 1024 * 1024) { if (streamBytes != null && mImageWidth > 0 && mImageHeight > 0) {
mCacheBitmap = BitmapFactory.decodeStream(stream); mSmallImage = mImageWidth * mImageHeight < 1024 * 1024;
mSmallImage = true; if (mSmallImage) {
} else { mCacheBitmap = BitmapFactory.decodeByteArray(streamBytes, 0, streamBytes.length, null);
mRegionDecoder = BitmapRegionDecoder.newInstance(stream, true); } else {
mCacheRect.setEmpty(); mRegionDecoder = BitmapRegionDecoder.newInstance(streamBytes, 0, streamBytes.length, true);
mSmallImage = false; mCacheRect.setEmpty();
}
} }
if (mCacheBitmap == null && mRegionDecoder == null) { if (mCacheBitmap == null && mRegionDecoder == null) {
String size = options.outWidth + "x" + options.outHeight; String message = errorMessage;
String message = "Stream could not be decoded. Image size: " + size; if (message == null) {
if (mImageWidth <= 0 || mImageHeight <= 0) message = "Stream could not be decoded.";
throw new IllegalArgumentException(message); if (mImageWidth > 0 && mImageHeight > 0) {
else message += " Image size: " + mImageWidth + "x" + mImageHeight;
throw new IOException(message); }
}
throw new Exception(message);
} }
mImageOK = true; mImageOK = true;

Wyświetl plik

@ -119,41 +119,30 @@ public class MainActivity extends AppCompatActivity {
// Set verbose to false for any Uri that might have expired (e.g. shared from browser). // Set verbose to false for any Uri that might have expired (e.g. shared from browser).
private boolean loadImage(Uri uri, boolean verbose) { private boolean loadImage(Uri uri, boolean verbose) {
boolean succeeded = false;
ContentResolver resolver = getContentResolver(); ContentResolver resolver = getContentResolver();
InputStream stream = null;
if (uri != null) { if (uri != null) {
mSettings.setImageUri(uri);
try { try {
stream = resolver.openInputStream(uri); InputStream stream = resolver.openInputStream(uri);
if (stream != null)
mCropView.setBitmap(stream);
succeeded = true;
} catch (Exception ex) { // e.g. FileNotFoundException, SecurityException } catch (Exception ex) { // e.g. FileNotFoundException, SecurityException
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isPermissionException(ex) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isPermissionException(ex)
&& needsRequestReadPermission()) { && needsRequestReadPermission()) {
requestReadPermission(REQUEST_LOAD_IMAGE_PERMISSION); requestReadPermission(REQUEST_LOAD_IMAGE_PERMISSION);
return false;
} }
showFileNotLoadedMessage(ex, verbose); else
showFileNotLoadedMessage(ex, verbose);
} }
} }
if (stream == null || !loadImage(stream, resolver, uri)) { if (succeeded) {
mCropView.rotateImage(getOrientation(resolver, uri));
mSettings.setImageUri(uri);
}
else
setDefaultBitmap(); setDefaultBitmap();
return false; return succeeded;
}
return true;
}
private boolean loadImage(InputStream stream, ContentResolver resolver, Uri uri) {
try {
mCropView.setBitmap(stream);
} catch (IllegalArgumentException ex) {
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
return false;
} catch (Exception ex) {
String s = Utility.createMessage(ex) + "\n\n" + uri;
showErrorMessage(getString(R.string.load_img_err_title), ex.getMessage(), s);
return false;
}
mCropView.rotateImage(getOrientation(resolver, uri));
return true;
} }
private void setDefaultBitmap() { private void setDefaultBitmap() {