Android Question How to copy a scaled picture into Database

lucdrb

Active Member
Licensed User
Longtime User
Hi,

I use a picture from the phone and I modified it.

My problem is: how to save it into my DB (table: photo (id_photo INT,name_photo TEXT, photo BLOB)).

I saw the following code a lot of time on the forum :

'convert the image file to a bytes array
Dim InputStream1 As InputStream
InputStream1 = File.OpenInput(File.DirAssets, "pallette.png")

Dim OutputStream1 As OutputStream
OutputStream1.InitializeToBytesArray(1000)

File.Copy2(InputStream1, OutputStream1)

Dim Buffer() As Byte 'declares an empty array
Buffer = OutputStream1.ToBytesArray

'write the image to the database
SQL1.ExecNonQuery2

But it doesn't explain how to save a modified picture.

Thanks in advance for your help
Luc
 

alienhunter

Active Member
Licensed User
Longtime User

Hi

you want to update the same field in the database or write in a different place in the database ?



insert if you just have those 3 col's in your database/table
SQL1.ExecNonQuery2("INSERT INTO table1 VALUES(?, ?, ?)", ArrayAsObject( id, yourname, yourpicture))
 
Last edited:
Upvote 0

lucdrb

Active Member
Licensed User
Longtime User
hi,

I want to insert a new field into the database.
The problem isn't the sqlite request, I didn't see how to prepare the outpustream to save the Blob

Luc
 
Upvote 0

alienhunter

Active Member
Licensed User
Longtime User
hi,

I want to insert a new field into the database.
The problem isn't the sqlite request, I didn't see how to prepare the outpustream to save the Blob

Luc


you just save the buffer

http://www.b4x.com/android/forum/threads/sql-tutorial.6736/#content

B4X:
Sub InsertBlob
    'convert the image file to a bytes array
    Dim InputStream1 As InputStream
    InputStream1 = File.OpenInput(File.DirAssets, "smiley.gif")
    Dim OutputStream1 As OutputStream
    OutputStream1.InitializeToBytesArray(1000)
    File.Copy2(InputStream1, OutputStream1)
    Dim Buffer() As Byte 'declares an empty array
    Buffer = OutputStream1.ToBytesArray
   
    'write the image to the database
    SQL1.ExecNonQuery2("INSERT INTO table2 VALUES('smiley', ?)", Array As Object(Buffer))
End Sub
 
Upvote 0

lucdrb

Active Member
Licensed User
Longtime User
Ok that's what I allready saw in the forum but it doesn't help me to do what I want.

Hereafter what's I'm doing:
1. I take a .png file name logo.png from the file.dirAssets with the code:
- dim photo as bitmap
- photo.Initialize(File.DirAssets,"logo.png")
2. I show this logo.png into an imageview and before, I modify it (scale it, put it in black and white ....)
- sub Scale.... end sub
- sub blackandwhite...... end sub
- imageview1.bitmap = photo

I want to save the bitmap named photo(the logo.png modified) into my database.

How to make it ??

Thanks in advance

Luc
 
Upvote 0

nwhitfield

Active Member
Licensed User
Longtime User
This may help; the bitmap in this case isn't something I've edited, but one I've fetched from a server using httputils2, and so is available as a bitmap in Job.getBitmap, so first

B4X:
Dim pictureData As Bitmap
pictureData = job.GetBitmap

Then, sequence was part of the job name, which lets me know which member's photo I'm dealing with, and I either insert or update the database like this:


B4X:
Dim cache As Cursor
                    cache = BLUFcache.ExecQuery2("SELECT memberid FROM thumbs WHERE memberid = ?",Array As String(sequence))
                    Dim needThumb As Boolean = False
                    If cache.RowCount = 0 Then
                        Log("SVC No entry in database for api thumb " & sequence)
                        ' no entry in the database for this member
                        ' we'll save the api thumb
                        needThumb = True
                    End If
                   
                    Dim idString As String = NumberFormat(sequence,4,0)
                    Dim tmpFile As OutputStream
                    tmpFile = File.OpenOutput(File.DirInternalCache,"A" & idString & ".png",False)
                    pictureData.WriteToStream(tmpFile,100,"PNG")
                    tmpFile.Close
                    Log("Saved api thumb")
                   
                    Dim thumbData() As Byte
                    Dim inS As InputStream
                    Dim outS As OutputStream
                   
                    inS = File.OpenInput(File.DirInternalCache,"A" & idString & ".png")
                    outS.InitializeToBytesArray(1000)
                    File.Copy2(inS,outS)
                   
                    thumbData = outS.ToBytesArray
                    If needThumb = False Then
                        ' already have an entry in the database, so just update it with the api thumb
                        Log("SVC Updating existing record for api thumb")
                        BLUFcache.AddNonQueryToBatch("UPDATE thumbs SET apiThumb = ? WHERE memberid = " & sequence, Array As Object(thumbData))
                    Else
                        ' no entry, so make a new one
                        Log("SVC Creating new record for apithumb")
                       
                        Dim now As Long
                        now = (DateTime.now/1000) - Main.timeOffset ' convert back to UTC
                        BLUFcache.AddNonQueryToBatch("INSERT INTO thumbs VALUES ( ?, null, ?, ? , ?)", Array As Object(sequence,thumbData,now,now))
                    End If
                    Main.batchpending = True

Looking at this code now, it may not be the most efficient... but effectively I write the data to a file as a PNG; I then read it back in from that PNG as copy it from the disk to a bytes array, which can then be stuck in the database as a blob.

So, this is really doing much the same thing as the previous (shorter) code example, except that it's starting with a bitmap, saving it, then reading it into the bytes array. I've never tried to see if you can use Bitmap.WriteToStream with an output stream that's initialised to a bytes array, as I wanted the file anyway.
 
Upvote 0

lucdrb

Active Member
Licensed User
Longtime User
Thanks you nwhitfield It's help a lot.

I don't know how to make it most efficient but It's work and it's what I searched.

Luc
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…