Show error instead of send it, if image size is invalid (e.g. -1 x -1)

pull/3/head
Olga Miller 2017-01-07 14:59:12 +01:00
rodzic 82710aa7e2
commit 2e68bb431a
2 zmienionych plików z 10 dodań i 3 usunięć

Wyświetl plik

@ -180,7 +180,7 @@ public class CropView extends ImageView {
invalidate(); invalidate();
} }
public void setBitmap(@NonNull InputStream stream) throws IOException { public void setBitmap(@NonNull InputStream stream) throws IOException, IllegalArgumentException {
mImageOK = false; mImageOK = false;
mOrientation = 0; mOrientation = 0;
recycle(); recycle();
@ -188,7 +188,7 @@ public class CropView extends ImageView {
invalidate(); invalidate();
} }
private void loadImage(InputStream stream) throws IOException { private void loadImage(InputStream stream) throws IOException, IllegalArgumentException {
// app6 + exif // app6 + exif
int bufferBytes = 1048576; int bufferBytes = 1048576;
if (!stream.markSupported()) if (!stream.markSupported())
@ -212,7 +212,11 @@ public class CropView extends ImageView {
if (mCacheBitmap == null && mRegionDecoder == null) { if (mCacheBitmap == null && mRegionDecoder == null) {
String size = options.outWidth + "x" + options.outHeight; String size = options.outWidth + "x" + options.outHeight;
throw new IOException("Stream could not be decoded. Image size: " + size); String message = "Stream could not be decoded. Image size: " + size;
if (mImageWidth <= 0 || mImageHeight <= 0)
throw new IllegalArgumentException(message);
else
throw new IOException(message);
} }
mImageOK = true; mImageOK = true;

Wyświetl plik

@ -130,6 +130,9 @@ public class MainActivity extends AppCompatActivity {
private boolean loadImage(InputStream stream, ContentResolver resolver, Uri uri) { private boolean loadImage(InputStream stream, ContentResolver resolver, Uri uri) {
try { try {
mCropView.setBitmap(stream); mCropView.setBitmap(stream);
} catch (IllegalArgumentException ex) {
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
return false;
} catch (Exception ex) { } catch (Exception ex) {
String s = Utility.createMessage(ex) + "\n\n" + uri; String s = Utility.createMessage(ex) + "\n\n" + uri;
showErrorMessage(getString(R.string.load_img_err_title), ex.getMessage(), s); showErrorMessage(getString(R.string.load_img_err_title), ex.getMessage(), s);