检查图像视图是否为空

时间:2017-11-15 14:14:33

标签: android sqlite null imageview bitmapimage

在我的应用程序上,我通过相机或图库插入图像,然后将其转换为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();
}

那么如何检查imageview不为null ???提前致谢

1 个答案:

答案 0 :(得分:0)

您可以在此方法中使用try catch吗

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;
}