-2

i'm a beginner in android i'm make an app to insert data in sqlite database and i i have a null pointer exception when add the record to database

this code get the pic from imageview and convert it to bitmap then to byte to save it any suggest to avoid this exception here is the code

            imageView1.setDrawingCacheEnabled(true);
            imageView1.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));


            imageView1.layout(0, 0, imageView1.getMeasuredWidth(),  imageView1.getMeasuredHeight()); 

             imageView1.buildDrawingCache(true);

             Bitmap b = Bitmap.createBitmap(imageView1.getDrawingCache());
             imageView1.setDrawingCacheEnabled(false);

            save_picture=loginDataBaseAdapter.getBytes(b);


            loginDataBaseAdapter.insertEntry(userName, password, save_picture);
            Toast.makeText(getApplicationContext(), "Record created  ", Toast.LENGTH_LONG).show();`}

there is the method to convert bitmap to byte

// convert from bitmap to byte array
     public  byte[] getBytes(Bitmap bitmap) {
      ByteArrayOutputStream stream = new ByteArrayOutputStream();
      bitmap.compress(CompressFormat.PNG, 0, stream);
      return stream.toByteArray();
     }

and there is the insert method

public void insertEntry(String userName,String password , byte[] photo  )
    {
       ContentValues newValues = new ContentValues();
        // Assign values for each row.
        newValues.put("USERNAME", userName);
        newValues.put("PASSWORD",password);
        newValues.put("Photo", photo);

        // Insert the row into your table
        db.insert("LOGIN", null, newValues);

    }

and there is the db creation

static final String DATABASE_CREATE = "create table "+"LOGIN"+
                                 "( " +"ID"+" integer primary key autoincrement,"+ "USERNAME  text,PASSWORD text, Photo blob not null); ";

Thanks

BOB91
  • 3
  • 4
  • Can we see the error output? – The Head Rush Dec 29 '14 at 19:48
  • 1
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – njzk2 Dec 29 '14 at 19:52
  • E/AndroidRuntime(15303): FATAL EXCEPTION: main E/AndroidRuntime(15303): java.lang.NullPointerException E/AndroidRuntime(15303): at android.graphics.Bitmap.createBitmap(Bitmap.java:571) E/AndroidRuntime(15303): at com.techblogon.loginexample.SignUPActivity$1.onClick(SignUPActivity.java:91) E/AndroidRuntime(15303): at android.view.View.performClick(View.java:2532) here is the error – BOB91 Dec 30 '14 at 11:32

3 Answers3

0

use this class to convert bitmap to byteArray:

public class Utility {
// convert from bitmap to byte array
public static byte[] getBytes(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0, stream);
    return stream.toByteArray();
}

// convert from byte array to bitmap
public static Bitmap getPhoto(byte[] image) {
    return BitmapFactory.decodeByteArray(image, 0, image.length);
}

} and into InsertEntry Method:

newValues.put(G_PHOTO, Utility.getBytes(image));
Ahmad Sanie
  • 3,678
  • 2
  • 21
  • 56
0

Try to save your bitmap in a base64 form string and then insert it to your database. So, your getBytes method will be getBase64Picture and will be like :

 public  String getBase64StrFromPicture(Bitmap bitmap) {
   ByteArrayOutputStream stream = new ByteArrayOutputStream();
   bitmap.compress(CompressFormat.PNG, 0, stream);
   String imageEncoded = Base64.encodeToString(stream.toByteArray(), Base64.DEFAULT);
   return imageEncoded;
 }

Then ,to retrieve your picture, after the query you could use the reverse procedure, aka transform your string-ed picture to a Bitmap!

 public Bitmap getBitmapFromBase64Str(){
   byte[] decodedString = Base64.decode(encodedImage, Base64.URL_SAFE);
   Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
   return decodedByte;
 }
karvoynistas
  • 1,295
  • 1
  • 14
  • 30
0

guys the null pointer exception because the getDrawingCache always return null

to avoid this error i use this method

  public static Bitmap loadBitmapFromView(View v) {
          Bitmap bi = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);                
          Canvas c = new Canvas(bi);
          v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
          v.draw(c);
          return bi;
     }

thanks for guide me :)

BOB91
  • 3
  • 4