0

How can I properly change this byte[] photo = getBytes(imageBitmap); to be stored in a database Blob?

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);

            Bitmap bmp = BitmapFactory.decodeByteArray(photo, 0, photo.length);
            ImageView image = (ImageView) findViewById(R.id.imageView);

            image.setImageBitmap(Bitmap.createScaledBitmap(bmp, image.getWidth(), image.getHeight(), false));


           // DatabaseHandler mydb = new DatabaseHandler(getApplicationContext());
           // Walk walk = new Walk(photo);
          //  mydb.addWalk(walk);
}
Music Backup
  • 59
  • 1
  • 6
  • Have you tried this solution? Looks like a similar problem https://stackoverflow.com/questions/11790104/how-to-storebitmap-image-and-retrieve-image-from-sqlite-database-in-android – Kafels Apr 15 '18 at 00:41
  • You should accept answers which work for you ... or tell people they don't work :) – MacD Apr 17 '18 at 22:27

2 Answers2

0

Have you tried to use the ByteArrayOutputStream? You can check at the documentation https://developer.android.com/reference/java/io/ByteArrayOutputStream.html

Or to make it ease you can do like this guy did converting Java bitmap to byte array

`

Bitmap bmp = intent.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
bmp.recycle();

`

0

First of all, what's the exact problem? Saving the image or storing it into the db?

CREATE TABLE t1 (id INTEGER PRIMARY KEY, data BLOB);

Comes down to this:

InputStream is = context.getResources().open(R.drawable.MyImageFile);
try {
    byte[] buffer = new byte[CHUNK_SIZE];
    int size = CHUNK_SIZE;
    while(size == CHUNK_SIZE) {
        size = is.read(buffer);  //read chunks from file
        if (size == -1) break;

        ContentValues cv = new ContentValues();
        cv.put(CHUNK, buffer);       //CHUNK blob type field of your table

        long rawId = database.insert(TABLE, null, cv); //TABLE table name
    }
} catch (Exception e) {
    Log.e(TAG, "Error saving raw image to: "+rawId, e);
}
MacD
  • 783
  • 7
  • 23