开发者

How to insert an image into sqlite database?

I have tried the following code :

1.Code to convert bitmap to byte array :

private byte[] bitmaptoByteArray( Bitmap bmp)
      {
          ByteArrayOutputStream stream = new ByteArrayOutputStream();
          bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
          byte[] byteArray = stream.toByteArray();
          return byteArray;
      }

2.Code which to insert the values into 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();
            }
      }

I get nullpointer exception.

Call from the function to put data into DB :

   insertIntoImageTable( 7 , productname[0] , productdesc[0] , barry);

All the three bits of code are in the same class file.

Edit: The stack trace is as below.

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.in开发者_StackOverflow中文版voke(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)


It looks like your database is null or productname[0],productdesc[0]orbarry` is null.

In general when saving large files on Android, you should save the file to disk (maybe the SD card if it's available) and then store the file URI in the database. The SQLite database on Android is not really designed for storing large content - you'll getter performance storing large files directly.


Have you instantiated your dataBaseHelper and checked the database is created?

Your getting a NullPointerException on the sqlQueries.java class line 101


Did you initialize your db ?

db.insert(dataBaseHelper.IMAGE_DATA, null, values);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜