Android Question Bitmaps To SQL Database Without Cacheing

RichardN

Well-Known Member
Licensed User
Longtime User
I have a routine that reads a sequence of 640x480 bitmaps from an SQL database to resize them to a thumbnail size using the reflection method and then write them back to another field in the same SQL row.

There are a couple examples on the forum of reading the data, caching the bitmap to disk and then reading the file into an inputstream before writing to SQL.

I cannot however see an example of creating a picture buffer from a bitmap without going via a cached file..... Any offers?
 

RichardN

Well-Known Member
Licensed User
Longtime User
Ciao Tigrot, thanks but that is not the issue. Resizing the bitmap is simple using this code...

B4X:
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

How do you convert a bitmap to an array of bytes so that it can be saved to an SQL Db? My guess is you copy in/out streams but I can't arrive at a method that works.
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
Not so difficult after all.....
B4X:
bmp = CreateScaledBitmap(bmp, 112, 150, True)                'create the thumbnail

Dim Buffer() As Byte
Dim out As OutputStream

out.InitializeToBytesArray(0)
bmp.WriteToStream(out,100,"PNG")    'This statement can confuse.  It has nothing to do
                                    'with preparing to write PNG files, it is merely an avenue to the stream    
out.Close
Buffer = out.ToBytesArray

        'Use an 'UPDATE/WHERE to write the Buffer to a Blob field in the database
 
Upvote 0
Top