0

I am very close to having this work. I get a picture from the camera and store it in my SQLite database. However when I store it, I get this error. The byte array for the image is null when stored. Why? How can I fix it?

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.psa.stepbystep, PID: 16363 java.lang.NullPointerException: Attempt to get length of null array at com.example.psa.stepbystep.Walk.writeToParcel(Walk.java:161) at android.os.Parcel.writeParcelable(Parcel.java:1730) at android.os.Parcel.writeValue(Parcel.java:1636) at android.os.Parcel.writeArrayMapInternal(Parcel.java:777) at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1506)

Walk Activity contains Parcel

public Walk(Parcel in) {
        this._photo = new byte[in.readInt()];
        in.readByteArray(_photo);
    }

public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(_photo.length);
        dest.writeByteArray(_photo);
    }

WalkPhoto has this to convert

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

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

WalkPhoto also has this to store the image in the database

     if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            ImageView imageview = findViewById(R.id.imageView);
            imageview.setImageBitmap(imageBitmap);

            //Bitmap to bytes for database
            byte[] photo = getBytes(imageBitmap);

             DatabaseHandler mydb = new DatabaseHandler(getApplicationContext());

            Walk walk = new Walk(photo);

            mydb.addWalk(walk);
}
RockOn365
  • 3
  • 1
  • 5

1 Answers1

1

You can use "blob" to store image as mention in this post: https://stackoverflow.com/a/9357943/5455940

In above post "insertStatement_logo" is instance of SQLiteStatement class. SQLiteStatement is subclass of SQLiteProgram class.

See links for reference. https://developer.android.com/reference/android/database/sqlite/SQLiteStatement.html

https://developer.android.com/reference/android/database/sqlite/SQLiteProgram.html

Anonymous
  • 2,184
  • 15
  • 23