lundi 30 novembre 2015

Image not loading from SQLite

In my application I am trying to take an image and display it in an ImageView, the image is being taken and added to the ImageView but not being stored or loaded into SQLite. I'm not sure why the image won't store as it does load into the ImageView.

Code to take the image:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Take a picture and pass the image along to onActivityResult
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);

startActivityForResult():

static final int REQUEST_IMAGE_CAPTURE = 1;    
callingActivity = (MainActivity) getActivity();

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
        // Get the photo
        Bundle extras = data.getExtras();
        Bitmap photo = (Bitmap) extras.get("data");
        imageView.setImageBitmap(photo);
        // Convert to byte array
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();

        // Add image to db here
        (callingActivity.dbManager).addImageToRecipe(byteArray, recipe);
    }
}

addImageToRecipe() is a method in my DBManager class:

public void addImageToRecipe(byte[] image, Recipe recipe) {

    SQLiteDatabase db = this.getWritableDatabase();

    db.beginTransaction();
    try {
        ContentValues values = new  ContentValues();
        values.put(COLUMN_IMAGE_ID, image);
        db.update(TABLE_RECIPES, values, COLUMN_RECIPE_NAME + "=\"" + recipe.getRecipeTitle() + "\"", null);
    } finally {
        db.endTransaction();
    }
    db.close();
}

Setting the ImageView (getImageId returns a byte[]). It never goes into the if statement in all the times I have run it, suggesting that the value is null.

if (recipe.getImageId() != null) {
        Bitmap bmp = BitmapFactory.decodeByteArray(recipe.getImageId(), 0, recipe.getImageId().length);
        imageView.setImageBitmap(bmp);
    } else {

    }

Im not sure what to do to fix it to get it to load the image into the database and back from it into the ImageView.

Aucun commentaire:

Enregistrer un commentaire