我曾嘗試下面的代碼:如何將圖像插入到sqlite數據庫?
1.Code以位圖轉換爲字節數組:
private byte[] bitmaptoByteArray(Bitmap bmp)
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
2.Code其中值插入到DB
public void insertIntoImageTable(int companyid , String productname , String productdesc , byte[] bmp)
{
ContentValues values = new ContentValues();
values.put(dataBaseHelper.COMPANY_ID, companyid);
values.put(dataBaseHelper.PRODUCT_NAME, productname);
values.put(dataBaseHelper.PRODUCT_DESCRIPTION, productdesc);
values.put(dataBaseHelper.PRODUCT_IMAGE, bmp);
try
{
db.insert(dataBaseHelper.IMAGE_DATA, null, values);
}
catch(Exception e)
{
e.printStackTrace();
}
}
我得到空指針例外。從功能
通話將數據放入DB:
insertIntoImageTable(7 , productname[0] , productdesc[0] , barry);
所有代碼的三位在同一個類文件。
編輯:堆棧跟蹤如下。
03-24 16:04:26.245: WARN/System.err(13255): java.lang.NullPointerException
03-24 16:04:26.265: WARN/System.err(13255): at com.omkarsoft.ImagePortofolio.sqlQueries.insertIntoImageTable(sqlQueries.java:101)
03-24 16:04:26.275: WARN/System.err(13255): at com.omkarsoft.ImagePortofolio.sqlQueries.upLoadtoDatabase(sqlQueries.java:118)
03-24 16:04:26.305: WARN/System.err(13255): at com.omkarsoft.ImagePortofolio.ImageAdapter.<init>(ImageAdapter.java:58)
03-24 16:04:26.315: WARN/System.err(13255): at com.omkarsoft.ImagePortofolio.imageGridView.onCreate(imageGridView.java:42)
03-24 16:04:26.335: WARN/System.err(13255): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-24 16:04:26.335: WARN/System.err(13255): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-24 16:04:26.355: WARN/System.err(13255): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-24 16:04:26.385: WARN/System.err(13255): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-24 16:04:26.385: WARN/System.err(13255): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-24 16:04:26.415: WARN/System.err(13255): at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 16:04:26.415: WARN/System.err(13255): at android.os.Looper.loop(Looper.java:123)
03-24 16:04:26.435: WARN/System.err(13255): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-24 16:04:26.445: WARN/System.err(13255): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 16:04:26.455: WARN/System.err(13255): at java.lang.reflect.Method.invoke(Method.java:507)
03-24 16:04:26.475: WARN/System.err(13255): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-24 16:04:26.475: WARN/System.err(13255): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-24 16:04:26.505: WARN/System.err(13255): at dalvik.system.NativeStart.main(Native Method)
請示例。每當你有一個時就做。 – 2011-03-24 11:16:07
似乎你有db變量null。 – 2011-03-24 11:16:45
它是一個空指針異常 – pradeep 2011-03-24 11:16:46