0

My Android-App reads image-files from the sd-card and stores the image in a blob in a sqlite database.

Currently i am converting a FileInputStream to a byte array and store this in the blob. A blob cannot exceed the size of 1MB, so in this case i am posting an error-message and cancel the operation.

        FileInputStream fis = null;
    try {
        fis = new FileInputStream(FilePath);    // FilePath contains a valid path
    } catch (FileNotFoundException e) {
        Toast.makeText(MyActivity.this, "File not found: " + FilePath, Toast.LENGTH_LONG).show();
        return;
    }

    BufferedInputStream bis = new BufferedInputStream(fis, 1070);
    ByteArrayBuffer bab = new ByteArrayBuffer(128);

    int current = 0;
    try {
        while ((current = bis.read()) != -1) bab.append((byte) current);
    } catch (IOException e) {
        Toast.makeText(MyActivity.this, "Error reading Picture: " + FilePath, Toast.LENGTH_LONG).show();
        return;
    }

    byte[] imageBa = bab.toByteArray();

    if (imageBa.length > 1024*1024) 
        showErrorDialog();
    else { 
        saveImageInDatabase(imageBa); // sage image byte[] in BLOB-column so sq-lite database

        // show image in imageView
        imageStream = new ByteArrayInputStream(imageBa);
        Bitmap imageBitmap = BitmapFactory.decodeStream(imageStream);   // uncompressed imageBitmap has a much bigger size than the image-byte[]
        imageView.setImageBitmap(imageBitmap);
    }

I want to get rid of the 1MB-limitation and store also bigger images, by reducing the resolution (not the size).

I could go for a solution using the BitmapFactory option inSampleSize to compress an image and / or convert the bitmap back to a byte[], e.g. using bitmap.compress. However, even an uncompressed bitmap created with BitmapFactory has a much bigger size than the original byte [], so i fear that i lose quality.

Any ideas how to solve my issue? Many thanks in advance, Gerhard.

gerhard
  • 479
  • 1
  • 6
  • 15

0 Answers0