Skip to content

Commit

Permalink
Merge pull request #4509 from pingwang2011/timob-14540
Browse files Browse the repository at this point in the history
timob-14540: Android: blob.imageAsResize causes java error
  • Loading branch information
ayeung committed Aug 16, 2013
2 parents 5796c8b + d053f14 commit 8d7808e
Showing 1 changed file with 59 additions and 22 deletions.
81 changes: 59 additions & 22 deletions android/titanium/src/java/org/appcelerator/titanium/TiBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -487,12 +487,17 @@ public Bitmap getImage()
// If the image is not available but the width and height of the image are successfully fetched, the image can
// be created by decoding the data.
if (image == null && (width > 0 && height > 0)) {
switch (type) {
case TYPE_FILE:
return BitmapFactory.decodeStream(getInputStream());
case TYPE_DATA:
byte[] byteArray = (byte[]) data;
return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
try {
switch (type) {
case TYPE_FILE:
return BitmapFactory.decodeStream(getInputStream());
case TYPE_DATA:
byte[] byteArray = (byte[]) data;
return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
}
} catch (OutOfMemoryError e) {
Log.e(TAG, "Unable to get the image. Not enough memory: " + e.getMessage(), e);
return null;
}
}
return image;
Expand All @@ -515,8 +520,13 @@ public TiBlob imageAsCropped(Object params)
int heightCropped = options.optInt(TiC.PROPERTY_HEIGHT, height);
int x = options.optInt(TiC.PROPERTY_X, (width - widthCropped) / 2);
int y = options.optInt(TiC.PROPERTY_Y, (height - heightCropped) / 2);
Bitmap imageCropped = Bitmap.createBitmap(img, x, y, widthCropped, heightCropped);
return blobFromImage(imageCropped);
try {
Bitmap imageCropped = Bitmap.createBitmap(img, x, y, widthCropped, heightCropped);
return blobFromImage(imageCropped);
} catch (OutOfMemoryError e) {
Log.e(TAG, "Unable to crop the image. Not enough memory: " + e.getMessage(), e);
return null;
}
}

@Kroll.method
Expand All @@ -529,8 +539,13 @@ public TiBlob imageAsResized(Number width, Number height)

int dstWidth = width.intValue();
int dstHeight = height.intValue();
Bitmap imageResized = Bitmap.createScaledBitmap(img, dstWidth, dstHeight, true);
return blobFromImage(imageResized);
try {
Bitmap imageResized = Bitmap.createScaledBitmap(img, dstWidth, dstHeight, true);
return blobFromImage(imageResized);
} catch (OutOfMemoryError e) {
Log.e(TAG, "Unable to resize the image. Not enough memory: " + e.getMessage(), e);
return null;
}
}

@Kroll.method
Expand All @@ -543,7 +558,6 @@ public TiBlob imageAsThumbnail(Number size, @Kroll.argument(optional = true) Num
}

int thumbnailSize = size.intValue();
Bitmap imageThumbnail = ThumbnailUtils.extractThumbnail(img, thumbnailSize, thumbnailSize);

float border = 1f;
if (borderSize != null) {
Expand All @@ -554,12 +568,20 @@ public TiBlob imageAsThumbnail(Number size, @Kroll.argument(optional = true) Num
radius = cornerRadius.floatValue();
}

if (border == 0 && radius == 0) {
return blobFromImage(imageThumbnail);
}
try {
Bitmap imageThumbnail = ThumbnailUtils.extractThumbnail(img, thumbnailSize, thumbnailSize);

Bitmap imageThumbnailBorder = TiImageHelper.imageWithRoundedCorner(imageThumbnail, radius, border);
return blobFromImage(imageThumbnailBorder);
if (border == 0 && radius == 0) {
return blobFromImage(imageThumbnail);
}

Bitmap imageThumbnailBorder = TiImageHelper.imageWithRoundedCorner(imageThumbnail, radius, border);
return blobFromImage(imageThumbnailBorder);

} catch (OutOfMemoryError e) {
Log.e(TAG, "Unable to get the thumbnail image. Not enough memory: " + e.getMessage(), e);
return null;
}
}

@Kroll.method
Expand All @@ -570,8 +592,13 @@ public TiBlob imageWithAlpha()
return null;
}

Bitmap imageWithAlpha = TiImageHelper.imageWithAlpha(img);
return blobFromImage(imageWithAlpha);
try {
Bitmap imageWithAlpha = TiImageHelper.imageWithAlpha(img);
return blobFromImage(imageWithAlpha);
} catch (OutOfMemoryError e) {
Log.e(TAG, "Unable to get the image with alpha. Not enough memory: " + e.getMessage(), e);
return null;
}
}

@Kroll.method
Expand All @@ -588,8 +615,13 @@ public TiBlob imageWithRoundedCorner(Number cornerRadius, @Kroll.argument(option
border = borderSize.floatValue();
}

Bitmap imageRoundedCorner = TiImageHelper.imageWithRoundedCorner(img, radius, border);
return blobFromImage(imageRoundedCorner);
try {
Bitmap imageRoundedCorner = TiImageHelper.imageWithRoundedCorner(img, radius, border);
return blobFromImage(imageRoundedCorner);
} catch (OutOfMemoryError e) {
Log.e(TAG, "Unable to get the image with rounded corner. Not enough memory: " + e.getMessage(), e);
return null;
}
}

@Kroll.method
Expand All @@ -601,7 +633,12 @@ public TiBlob imageWithTransparentBorder(Number size)
}

int borderSize = size.intValue();
Bitmap imageWithBorder = TiImageHelper.imageWithTransparentBorder(img, borderSize);
return blobFromImage(imageWithBorder);
try {
Bitmap imageWithBorder = TiImageHelper.imageWithTransparentBorder(img, borderSize);
return blobFromImage(imageWithBorder);
} catch (OutOfMemoryError e) {
Log.e(TAG, "Unable to get the image with transparent border. Not enough memory: " + e.getMessage(), e);
return null;
}
}
}

0 comments on commit 8d7808e

Please sign in to comment.