0

I'm new to Android development and I'm trying to insert an image into a database but its not working. Below is some of my code:

On click Code

public void click(View v){
        img= DatabaseHandler.getBytes(BitmapFactory.decodeResource(getResources(), R.drawable.test));

         DatabaseHandler.insertFood("test","test",10.00,img);
         Toast toast =  Toast.makeText(getApplicationContext(), "TEST", Toast.LENGTH_LONG);
        toast.show();

    }

DatabaseHandler Class

   public static long insertFood( String Name, String Description, Double Price,Byte[] Image ){
        ContentValues cv = new ContentValues();
        cv.put(nam,Name);
        cv.put(descrp, Description);
        cv.put(prc, Price);
        cv.put(img, Image);
        return sdb.insert(TABLE, null, cv);
    }
    public static byte[] getBytes(Bitmap bitmap) {
          ByteArrayOutputStream stream = new ByteArrayOutputStream();
          bitmap.compress(CompressFormat.PNG, 0, stream);
          return stream.toByteArray();
         }  

The error
enter image description here

Image 2 enter image description here

10
  • where is the lower case "price" / "description" coming from? global constants? Also you need a String key for image. You appear to be using lower case "image" as both key and value. Commented Feb 7, 2014 at 2:16
  • I just edited the first argument thats not their real name Commented Feb 7, 2014 at 2:18
  • well look at where you are putting image, you have the same variable twice. The first one needs to be the string key, the second the byte array. Commented Feb 7, 2014 at 2:22
  • 1
    Could you extend the error stack trace to "Cause by: blabla" from your LogCat? The current LogCat doesn't really help. Commented Feb 7, 2014 at 2:33
  • 1
    Your database variable (sdb) is null. That's the problem. You need to post the code where your opening the database if you need further help. p.s. you'll run into null pointers and similar problems a ton, look on the logcat for the line that relates to code in your program (e.g. line 82 of DatabaseHandler), to find these errors. just double click it, and it will take you right to that line. Commented Feb 7, 2014 at 2:45

1 Answer 1

1

OP just needed to post a longer stack trace, to see he was trying to insert to a null database. Credit to Andrew T. for asking user to post the longer trace.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.