检查图像视图是否不为空



在我的应用程序上,我通过相机或图库插入图像,然后将其转换为byte[]以保存在sqlite数据库中。插入时,我检查图像视图是否不为空,这是错误

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap(('

这是我的代码(插入到sqlite(

private void xuLyLuuTatCa() {
    String rangbuoc = etxtname.getText().toString();
    Bitmap bitmap = ((BitmapDrawable)imghinh3.getDrawable()).getBitmap();
    if(rangbuoc != null && rangbuoc.length() > 0
            || etxtadd.getText().toString() != null && etxtadd.getText().toString().length() > 0
            || etxtprice.getText().toString() != null && etxtprice.getText().toString().length() > 0
            || etxtopen.getText().toString() != null && etxtopen.getText().toString().length() > 0
            || etxtrate.getText().toString() != null && etxtrate.getText().toString().length() > 0
            || etxtdanhgia.getText().toString() != null && etxtdanhgia.getText().toString().length() > 0
            || imghinh1.getDrawable() != null || imghinh2.getDrawable() != null || bitmap != null) {
        db = database.initDatabase(this,DATABASE_NAME);
        String name = etxtname.getText().toString();
        String add = etxtadd.getText().toString();
        String price = etxtprice.getText().toString();
        String gio = etxtopen.getText().toString();
        double danhgia = Double.parseDouble(etxtrate.getText().toString());
        String binhluan = etxtdanhgia.getText().toString();
        byte[] anh = ConverttoArrayByte(imghinh1);
        byte[] anh2 = ConverttoArrayByte(imghinh2);
        byte[] anh3 = ConverttoArrayByte(imghinh3);
        boolean thich = false;
        ContentValues values = new ContentValues();
        //values.put("IDQuan", id);
        values.put("TenQuan", name);
        values.put("DiaChi", add);
        values.put("Gia", price);
        values.put("GioMoCua", gio);
        values.put("DanhGia", danhgia);
        values.put("Review", binhluan);
        values.put("YeuThich", thich);
        values.put("HinhAnh", anh);
        values.put("Hinh1", anh2);
        values.put("Hinh2", anh3);
        values.put("ViDo", 0);
        values.put("KinhDo", 0);
        String mess = "";
        if(db.insert("Quan",null,values) == -1)
        {
            mess = "Lỗi";
        }
        else
            mess = "Thành công";
        Toast.makeText(this, mess, Toast.LENGTH_SHORT).show();
        db.close();
        finish();
    }
    else
    {
        Toast.makeText(this, "Mời bạn nhập vào mục còn trống", Toast.LENGTH_LONG).show();
    }
}
public byte[] ConverttoArrayByte(ImageView img)
{
    BitmapDrawable bitmapDrawable = (BitmapDrawable) img.getDrawable();
    Bitmap bitmap=bitmapDrawable.getBitmap();
    ByteArrayOutputStream stream=new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    return stream.toByteArray();
}

那么如何检查图像视图是否为空???提前致谢

你可以在你的方法中使用一个尝试捕获,就像这样

public byte[] ConverttoArrayByte(ImageView img) {
    try {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) img.getDrawable();
        Bitmap bitmap = bitmapDrawable.getBitmap();
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        return stream.toByteArray();
    } catch (NullPointerException e) {
        // do something
    }
    return null;
}

相关内容

  • 没有找到相关文章

最新更新