Android Question How to convert a Base64 string into a Image file

elitevenkat

Active Member
Licensed User
Longtime User
From Mysql table i have synchronized data containing BLOB field through php script using base64_encode function.
in my B4A app, in JOB_DONE sub i am decoding the BLOB data as follows
B4X:
Case GetA
                ProgressDialogShow("fetching default question from Server")
                Dim rows As List
                rows = parser.NextArray
            
               If rows.Size >0 Then   
                    Dim ListOfMaps As List
                    ListOfMaps.Initialize
  
                   For i = 0 To rows.Size - 1          
                       Dim m As Map        
                           
                        m =  rows.Get(i)
                       
                       m.Put("eimg", su.DecodeBase64(m.Get("eimg")))  ' eimg is the BLOB field
                       
                        ListOfMaps.Add(m)
   
                   Next
                   Job.Release                 
                    DBUtils.InsertMaps(SQL, "AMASTER", ListOfMaps)

then i am looping through the Sqlite table and saving the BLOB data to a file as below

B4X:
    Dim Buffer() As Byte
     
       For i = 0 To Cursor1.RowCount - 1
           Cursor1.Position = i
           
           Buffer=Cursor1.GetBlob("eimg")
            Dim InputStream1 As InputStream
            InputStream1.InitializeFromBytesArray(Buffer, 2, Buffer.Length)
           Dim ou As OutputStream
           ou=File.OpenOutput(File.DirRootExternal,i& ".png",True)
         
           File.Copy2(InputStream1,ou)
           ou.Flush
           ou.Close 
       Next
    End If
    Cursor1.Close

The png files are saved in the device. However i am unable to open the image files. The photo / alum viewer reports "unable to show image...." error

where is the problem ? in converting and storing BLOB data ?
 

elitevenkat

Active Member
Licensed User
Longtime User

yes the image data is corrupt. i am getting bitmap error .... after testing with your code. Wonder how you always conclude correctly !!!
 
Upvote 0