【发布时间】:2017-10-07 22:14:37
【问题描述】:
我有数据库,他们可以在其中输入图像作为字段之一。图片保存到手机上,URI保存到数据库中。
当用户决定不为某件商品添加图片时,我遇到了麻烦。
我正在尝试编写 if/else 语句,其中“if”条件检查光标的图像列是否为空。我只是不知道在 if 条件中输入什么内容。
这是我正在使用的代码。基本上我想这样做,如果光标的 COLUMN_WINE_IMAGE 为空,只需祝酒。如果不为null,则设置图片。
//This section turns the database's image URI stored in this cursor into a bitmap, and then sets the ImageView.
Bitmap bitmap = null;
try {
if (............){
Toast.makeText(this, "No image was taken for this item. (TOAST IS JUST TESTING PURPOSES)", Toast.LENGTH_SHORT).show();
}else{
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(cursor.getString(cursor.getColumnIndexOrThrow(WineContract.WineEntry.COLUMN_WINE_IMAGE))));
mFullImage.setImageBitmap(bitmap);
}
} catch (IOException e) {
e.printStackTrace();
}
这是我尝试过但没有成功的一个例子:
if (Uri.parse(cursor.getString(cursor.getColumnIndexOrThrow(WineContract.WineEntry.COLUMN_WINE_IMAGE))).equals(null)){
//This returns error: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jeremy.sqlwine/com.example.jeremy.sqlwine.DetailsActivity}: java.lang.NullPointerException: uriString
【问题讨论】: