Android Question How to reduce and then covert bitmap to BlOB

DickD

Active Member
Licensed User
I have retrieved a bitmap from the Gallery using the code in PickPicture_Click below. I need to convert this (I guess) to a byte array before I can put it into a SQLite BLOB. This works fine except that some of the bitmaps are too big for the BLOB so I have to reduce them. I use CreateScaledBitmap as shown below. However, this only reduces the bitmap and not the byte array. How do I get the reduced smallbmp into the byte array( or how do I reduce the size of the byte array buffer1).

A bigger question is ... am I using the right tool, SQLite BLOB, to store this information? Is there some other storage method I can use that doesn't run in to the size limitation of the BLOB or that lets me store the bitmap directly in a file without converting.

B4X:
Sub PickPicture_Click
chooser.Show("image/*", "Choose image")
End Sub

Sub chooser_Result(Success As Boolean, Dir As String, FileName As String)
Dim smallbmp As Bitmap
If Success Then
bmp.Initialize(Dir, FileName)
smallbmp = CreateScaledBitmap(bmp, 960,960, True)
UserImage.Bitmap = smallbmp
newBmp = True

Dim InputStream1 As InputStream
InputStream1 = File.OpenInput(Dir,FileName)
Dim OutputStream1 As OutputStream
OutputStream1.InitializeToBytesArray(1000)
File.Copy2(InputStream1, OutputStream1)
buffer1 = OutputStream1.ToBytesArray
Msgbox(buffer1.Length, "image size = ")
Else
ToastMessageShow("No image selected", True)
End If
End Sub

Sub CreateScaledBitmap(Original As Bitmap, Width As Int, Height As Int, Filter As Boolean) As Bitmap
Dim r As Reflector
Dim b As Bitmap
b = r.RunStaticMethod("android.graphics.Bitmap", "createScaledBitmap", _
Array As Object(Original, Width, Height, Filter), _
Array As String("android.graphics.Bitmap", "java.lang.int", "java.lang.int", "java.lang.boolean"))
Return b
End Sub
 

Mahares

Expert
Licensed User
Longtime User
You are using the same large file again for your stream instead of the newly generated smaller file. That is why it generated the large byte array. Try this:
B4X:
Sub chooser_Result(Success As Boolean, Dir As String, FileName As String)
    Dim smallbmp As Bitmap
    If Success Then
        bmp.Initialize(Dir, FileName)
        smallbmp = CreateScaledBitmap(bmp, 960,960, True)
        UserImage.Bitmap = smallbmp
        newBmp = True
        Dim OutputStream1 As OutputStream
        OutputStream1.InitializeToBytesArray(1000)
        smallbmp.WriteToStream(OutputStream1, 100, "JPEG")  'write the new smaller file to outstream
        buffer1 = OutputStream1.ToBytesArray
        OutputStream1.close
        Msgbox(buffer1.Length, "image size = ")
    Else
        ToastMessageShow("No image selected", True)
    End If
End Sub
 
Upvote 0

DickD

Active Member
Licensed User
You are using the same large file again for your stream instead of the newly generated smaller file. That is why it generated the large byte array. Try this:
B4X:
Sub chooser_Result(Success As Boolean, Dir As String, FileName As String)
    Dim smallbmp As Bitmap
    If Success Then
        bmp.Initialize(Dir, FileName)
        smallbmp = CreateScaledBitmap(bmp, 960,960, True)
        UserImage.Bitmap = smallbmp
        newBmp = True
        Dim OutputStream1 As OutputStream
        OutputStream1.InitializeToBytesArray(1000)
        smallbmp.WriteToStream(OutputStream1, 100, "JPEG")  'write the new smaller file to outstream
        buffer1 = OutputStream1.ToBytesArray
        OutputStream1.close
        Msgbox(buffer1.Length, "image size = ")
    Else
        ToastMessageShow("No image selected", True)
    End If
End Sub

That did the trick. Thanks.
 
Upvote 0
Top