-2

Ive been trying to save my image into the SQLite database. But I can't solve it by my own. I've already tried in my opinion everything, but it does not work. So my question is if ive got this method:

    private byte[] getBitmapAsByteArray(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    // Middle value is quality, but PNG is lossless, so it's ignored.
    bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
    return outputStream.toByteArray();

and i've got this to take a photo and pick image from gallery.

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if (requestCode == REQUEST_CAPTURE && resultCode == RESULT_OK){
        Bundle extras = data.getExtras();
        Bitmap photo = (Bitmap) extras.get("data");
        imageView_Fotak.setImageBitmap(photo);


    }
    else if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
        imageUri_vybrana = data.getData();
        imageView_VyberFotku.setImageURI(imageUri_vybrana);
        imageView_VyberFotku = getBitmapAsByteArray();

How should I do it to convert my image for example from gallery to a BLOB? What method i should call? I would be very grateful for any help!

J Sol
  • 67
  • 7
  • You can try saving your image in another way. Save the image on the internal storage, and get its path and save that path in your SQLite database. Next time you want that image, you can just get the path from the database and fetch that image from that path. Hope this helps – intellignt_idiot Jul 02 '16 at 07:21
  • Thank you for answer, but i need to save it in the form of BLOB. And my problem is i don't know how to convert my image even if i have method that should do it. I mean how should i call something like image.getsBitmapAsByteArray() .... – J Sol Jul 02 '16 at 07:25
  • Did you check this http://stackoverflow.com/questions/7331310/how-to-store-image-as-blob-in-sqlite-how-to-retrieve-it – intellignt_idiot Jul 02 '16 at 07:28
  • Yes, I've already checked this and hundred more articles, but still don't know. – J Sol Jul 02 '16 at 07:31
  • Is there any error you are seeing or anything null ? What is happening actually with your current code ? – intellignt_idiot Jul 02 '16 at 07:32
  • Actually i cant even run it... I don't know how to convert my specific image , how should i use the convert method – J Sol Jul 02 '16 at 07:34
  • Are you event getting the image or bitmap ? Are you able to set the picked image to UIImageView ? First do that. – intellignt_idiot Jul 02 '16 at 07:36
  • I can show my code. That would be very helpfull and i would be very gratefull. I think is something stupid but I'm starting to be desperate. – J Sol Jul 02 '16 at 07:38
  • Yes, you need to show the code – intellignt_idiot Jul 02 '16 at 07:39
  • I've already asked question here, but it's been for some reason blocked as duplicated. [link](http://stackoverflow.com/questions/38140486/android-save-image-from-camera-and-gallery-into-sqlite-database-in-form-of-bl) – J Sol Jul 02 '16 at 07:41
  • In that code, you haven't put the image in your method named "dbOperace.pridejInformace(db, Sirka_0m);" Where is the byte array here in the method ? To convert image to byte array , the below lines would do the work, which you have already written. The only missing thing is BITMAP. ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] byteArray = stream.toByteArray(); – intellignt_idiot Jul 02 '16 at 07:46
  • Excuse me, i know i looked like complete idiot but i'm not sure that i understand what you mean – J Sol Jul 02 '16 at 07:51
  • Still cant figured it out. Could you please write the code you mean. That would be really awesome! And very helpfull – J Sol Jul 02 '16 at 08:15
  • I've done this but it's show me an error `byte[] ObrazekGalerie = params[2]; SQLiteDatabase db = dbOperace.getWritableDatabase(); dbOperace.pridejInformace(db, Sirka_0m, ObrazekGalerie);` – J Sol Jul 02 '16 at 08:42
  • If you could send me your code , I can check it and fix it. – intellignt_idiot Jul 02 '16 at 08:44
  • By code I mean the project zip. – intellignt_idiot Jul 02 '16 at 08:45
  • That would be awesome man! You are really great and kind!! here is the project [link](https://drive.google.com/open?id=0B19AM1KNZyS7LWF2RjR5cmdaaDA) – J Sol Jul 02 '16 at 08:52
  • Okay, I will check it now – intellignt_idiot Jul 02 '16 at 08:56

1 Answers1

1

Here is the link to your updated project : https://www.dropbox.com/s/lsv947j7susfcer/Formular1.00.zip?dl=0

It works for the image from gallery, same way you can make it work for camera image also.

Check the image, you can see blob saved.

See the image

intellignt_idiot
  • 1,962
  • 2
  • 16
  • 23