LITE ORM problems in saving columns bytes

Asked

Viewed 62 times

4

My app the user can use the camera to take photo and need to save it in the database. To save in the database I convert the image into an array of bytes, however I noticed a problem in this process when the resolution of the camera is high (4:3 16 MB) does not work and when I leave lower as 16:9 (6 MB) works. I am using the Ormlite to save and it does not return any error exception when the camera is in high resolution, does anyone know what might be?

        Bitmap photo = null;
        File file = new File(mCurrentPhotoPath);
        photo = MediaStore.Images.Media.getBitmap(
        this.getActivity().getContentResolver(),Uri.fromFile(file));
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        photo.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        bytesImage = stream.toByteArray();

        CheckListPendente CheckListPendente2 = new CheckListPendente();
        CheckListPendente2.setId(checkListPendenteId);
        CheckListResposta resposta = new CheckListResposta();
        if (bytesImage != null) {
            resposta.setImageBytes(bytesImage);
        }
        checkListDao = CheckListRespostaDao(helper.getConnectionSource());
                    checkListDao.create(resposta);
  • Change the line to photo.compress(Bitmap.CompressFormat.JPEG, 0, stream); and it worked, you know the reason why?

  • The third parameter you passed is related to compression quality. Probably there must be memory overflow, you don’t have any stacktrace ?

1 answer

1

Well I did a search in that method

Bitmap.CompressFormat 

and I ended up finding some articles about it, the bad thing is that most are in English (I don’t know if you can read "good", but it’s worth taking a look and understanding)

LINK 1: https://stackoverflow.com/questions/16177191/bitmap-compressbitmap-compressformat-png-0-fout-making-image-size-bigger-tha

LINK 2: https://stackoverflow.com/questions/8417034/how-to-make-bitmap-compress-without-change-the-bitmap-size

LINK 3: https://stackoverflow.com/questions/47090820/how-to-predict-image-size-in-megabytes-after-jpeg-compression

LINK 4: https://stackoverflow.com/questions/36487971/how-to-compress-bitmap-as-jpeg-with-least-quality-loss-on-android

Well what can I say,

photo.compress(Bitmap.CompressFormat.JPEG, 100, stream);

This line of yours is compressing the image to JPEG format, and in its second method parameter:

compress(<1 parâmetro>,<2 parâmetro>,<3 parâmetro>)

Indicates compression type being 0 maxim and 100 minimum / almost nil (SEE LINK 2) maybe in your code when passing all returns of values hit the memory limit or return a value greater than the initial value of the image size (in MB/KB.. etc.) (SEE LINK 1 and 3)

What I recommend doing is

1 - convert all images to a standard resolution

2 - do the necessary processing (in your case convert the image into an array of bytes)

3 - be happy

NOTE: I won’t go into code because I don’t know so much about Ormlite, but I believe that with a little time and dedication you can do it yourself, it’s worth taking a look at link 2 because there it makes a conversion/ setting resolution.

OBS2: Link 4 shows how to compress the image in the smallest possible way, thinking about code and file size / variable would be lower values (which would not affect the memory limit in the application, it is worth taking a look)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.