Skip to content

Commit c393e73

Browse files
committed
add null check code #19
1 parent 79ee6fa commit c393e73

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

app/src/main/java/com/naver/android/helloyako/imagecropsample/CropActivity.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,23 @@ public void onClick(View v) {
128128
public void onClick(View v) {
129129
if (!imageCropView.isChangingScale()) {
130130
Bitmap b = imageCropView.getCroppedImage();
131-
bitmapConvertToFile(b);
131+
if (b != null) {
132+
bitmapConvertToFile(b);
133+
} else {
134+
Toast.makeText(CropActivity.this, R.string.fail_to_crop, Toast.LENGTH_SHORT).show();
135+
}
132136
}
133137
}
134138
});
135139
}
136140

137141
private boolean isPossibleCrop(int widthRatio, int heightRatio) {
138-
int bitmapWidth = imageCropView.getViewBitmap().getWidth();
139-
int bitmapHeight = imageCropView.getViewBitmap().getHeight();
142+
Bitmap bitmap = imageCropView.getViewBitmap();
143+
if (bitmap == null) {
144+
return false;
145+
}
146+
int bitmapWidth = bitmap.getWidth();
147+
int bitmapHeight = bitmap.getHeight();
140148
if (bitmapWidth < widthRatio && bitmapHeight < heightRatio) {
141149
return false;
142150
} else {

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<resources>
22
<string name="app_name">ImageCropSample</string>
33
<string name="can_not_crop">Can not crop</string>
4+
<string name="fail_to_crop">Fail to crop</string>
45
</resources>

imagecropview/src/main/java/com/naver/android/helloyako/imagecrop/view/ImageCropView.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -919,24 +919,40 @@ public void run() {
919919

920920
public Bitmap getCroppedImage() {
921921
CropInfo cropInfo = getCropInfo();
922-
922+
if (cropInfo == null) {
923+
return null;
924+
}
925+
Bitmap bitmap;
923926
if (imageFilePath != null) {
924-
return cropInfo.getCroppedImage(imageFilePath);
927+
bitmap = cropInfo.getCroppedImage(imageFilePath);
925928
} else {
926-
return cropInfo.getCroppedImage(getViewBitmap());
929+
bitmap = getViewBitmap();
930+
if (bitmap != null) {
931+
bitmap = cropInfo.getCroppedImage(bitmap);
932+
}
927933
}
934+
return bitmap;
928935
}
929936

930937
public CropInfo getCropInfo() {
931938
Bitmap viewBitmap = getViewBitmap();
939+
if (viewBitmap == null) {
940+
return null;
941+
}
932942
float scale = baseScale * getScale();
933943
RectF viewImageRect = getBitmapRect();
934944

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

938948
public Bitmap getViewBitmap() {
939-
return ((FastBitmapDrawable) getDrawable()).getBitmap();
949+
Drawable drawable = getDrawable();
950+
if (drawable != null) {
951+
return ((FastBitmapDrawable) drawable).getBitmap();
952+
} else {
953+
Log.e(LOG_TAG, "drawable is null");
954+
return null;
955+
}
940956
}
941957

942958
public void setGridInnerMode(int gridInnerMode) {

0 commit comments

Comments
 (0)