在显示java.lang.NullPointerException斑点 [英] java.lang.NullPointerException in Blob
本文介绍了在显示java.lang.NullPointerException斑点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的java.sql.Blob对象将图像保存在MySQL。
当我尝试数据库插入对象,我有异常显示java.lang.NullPointerException
I using java.sql.Blob object to save images in mySql. When i try insert object in database i have exception "Java.lang.NullPointerException"
下面是我的code:
@Override
protected Object doInBackground(Object... arg0) {
try {
Bitmap photo = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
Users user = new Users();
user.setName("haris");
下一步code误会我的异常。
try {
Blob blo = null;
blo.setBytes(1, getBytes(photo));
} catch (Exception e) {
System.out.println("Error:"+e.toString()); //There is my Exception
}
/
if (Korisnik_servis.Registracija(user)){ // Call my REST services
System.out.println("Registration succesful");
}
else
{
System.out.println("Registration not succesful");
}
} catch (Exception e) {
System.out.println("Error:"+e.toString());
}
return null;
}
为位图转换功能为byte []
Function for convert Bitmap to byte []
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, stream);
return stream.toByteArray();
}
我的问题是:如何成功地初始化Blob对象
推荐答案
我使用的这块code的存储在斑点的位图(从绘制转换):
I am using this piece of code to store bitmaps in blobs (converted from drawable):
db = this.getWDB();
ContentValues values = new ContentValues();
Drawable d = model.getAppIcon();
BitmapDrawable bitDw = ((BitmapDrawable) d);
Bitmap bitmap = bitDw.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] imageInByte = stream.toByteArray();
values.put(COLUMN_ICON_BLOB, imageInByte);
db.insert(TABLE_APPS, null, values);
db.close();
和从数据库检索使用该方法
and to retrieve it from database this method is used
public static Drawable convertByteArrayToDrawable( byte[] byteArrayToBeCOnvertedIntoBitMap) {
Bitmap bitMapImage = BitmapFactory.decodeByteArray(
byteArrayToBeCOnvertedIntoBitMap, 0,
byteArrayToBeCOnvertedIntoBitMap.length);
return new BitmapDrawable(bitMapImage);
}
这篇关于在显示java.lang.NullPointerException斑点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文