Skip to content

Commit

Permalink
add null check code #19
Browse files Browse the repository at this point in the history
  • Loading branch information
helloyako committed Jan 6, 2017
1 parent 79ee6fa commit c393e73
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,23 @@ public void onClick(View v) {
public void onClick(View v) {
if (!imageCropView.isChangingScale()) {
Bitmap b = imageCropView.getCroppedImage();
bitmapConvertToFile(b);
if (b != null) {
bitmapConvertToFile(b);
} else {
Toast.makeText(CropActivity.this, R.string.fail_to_crop, Toast.LENGTH_SHORT).show();
}
}
}
});
}

private boolean isPossibleCrop(int widthRatio, int heightRatio) {
int bitmapWidth = imageCropView.getViewBitmap().getWidth();
int bitmapHeight = imageCropView.getViewBitmap().getHeight();
Bitmap bitmap = imageCropView.getViewBitmap();
if (bitmap == null) {
return false;
}
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight();
if (bitmapWidth < widthRatio && bitmapHeight < heightRatio) {
return false;
} else {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<resources>
<string name="app_name">ImageCropSample</string>
<string name="can_not_crop">Can not crop</string>
<string name="fail_to_crop">Fail to crop</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -919,24 +919,40 @@ public void run() {

public Bitmap getCroppedImage() {
CropInfo cropInfo = getCropInfo();

if (cropInfo == null) {
return null;
}
Bitmap bitmap;
if (imageFilePath != null) {
return cropInfo.getCroppedImage(imageFilePath);
bitmap = cropInfo.getCroppedImage(imageFilePath);
} else {
return cropInfo.getCroppedImage(getViewBitmap());
bitmap = getViewBitmap();
if (bitmap != null) {
bitmap = cropInfo.getCroppedImage(bitmap);
}
}
return bitmap;
}

public CropInfo getCropInfo() {
Bitmap viewBitmap = getViewBitmap();
if (viewBitmap == null) {
return null;
}
float scale = baseScale * getScale();
RectF viewImageRect = getBitmapRect();

return new CropInfo(scale, viewBitmap.getWidth(), viewImageRect.top, viewImageRect.left, mCropRect.top, mCropRect.left, mCropRect.width(), mCropRect.height());
}

public Bitmap getViewBitmap() {
return ((FastBitmapDrawable) getDrawable()).getBitmap();
Drawable drawable = getDrawable();
if (drawable != null) {
return ((FastBitmapDrawable) drawable).getBitmap();
} else {
Log.e(LOG_TAG, "drawable is null");
return null;
}
}

public void setGridInnerMode(int gridInnerMode) {
Expand Down

0 comments on commit c393e73

Please sign in to comment.