Bug? recover Blob field image

Status
Not open for further replies.

manoel vieira

Member
Licensed User
Longtime User
There was a problem retrieving an image of a BLOB field that were stored by external programs, I am not able to recover in B4A, but the same database SQLite used in C # or PHP the image field is read normally., below is the routine I'm using:

.....


Sub ReadBlob


c = s.ExecQuery("SELECT myPhoto FROM catalog WHERE pk_product = 1")
c.Position = 0
Dim Buffer() As Byte 'declare an empty byte array
Buffer = c.GetBlob("myPhoto")
Dim InputStream1 As InputStream
InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
Dim Bitmap1 As Bitmap
Bitmap1.Initialize2(InputStream1) ' *.......... error at this point
InputStream1.Close
ListView1.AddTwoLinesAndBitmap("test","test",Bitmap1)

c.Close



End Sub

error message with:

LogCat connected to: emulator-5554
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
main_readblob (java line: 410)
java.lang.RuntimeException: Error loading bitmap.
at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:521)
at com.manoelneto.texana.main._readblob(main.java:410)
at com.manoelneto.texana.main._activity_create(main.java:341)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at com.manoelneto.texana.main.afterFirstLayout(main.java:102)
at com.manoelneto.texana.main.access$000(main.java:17)
at com.manoelneto.texana.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
 

imbault

Well-Known Member
Licensed User
Longtime User
Maybe your blob contains nothing, try adding :
B4X:
If Buffer <> Null And Buffer.Length >1 Then
 

manoel vieira

Member
Licensed User
Longtime User
conditional was used, although not shown here, but the above code is inside the IF .....
If Buffer <> Null And buffer.Length> 1 Then ....

however the error happens ...
 

manoel vieira

Member
Licensed User
Longtime User
Sub ReadBlob

c = s.ExecQuery("SELECT item FROM produto WHERE pk_produto = 1")
c.Position = 0
Dim Buffer() As Byte 'declare an empty byte array
Buffer = c.GetBlob("item")

If Buffer <> Null And Buffer.Length >1 Then



Dim InputStream1 As InputStream
InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)


Dim Bitmap1 As Bitmap
Bitmap1.Initialize2(InputStream1) 'ERROR HERE
InputStream1.Close

ListView1.AddTwoLinesAndBitmap("test","teste",Bitmap1)



End If

c.Close

End Sub
 

manoel vieira

Member
Licensed User
Longtime User
Erel, thanks for the tips, I am new to the forum.
My problem is the fact that my SQLite table is accessed by other applications, such as C # or PHP and the image stored in the BLOB field works perfectly, but when I use B4A to access them from error ...

follows the code that accesses the field that contains the image:


B4X:
   c = s.ExecQuery("SELECT myPhoto FROM catalog WHERE pk_produto = 1")
   c.Position = 0
   Dim Buffer() As Byte 'declare an empty byte array
   Buffer = c.GetBlob("myPhoto")
   
   
    If Buffer <> Null And Buffer.Length >1 Then
       

     
       Dim InputStream1 As InputStream
       InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
       
           
        Dim Bitmap1 As Bitmap
       Bitmap1.Initialize2(InputStream1) 'ERROR HERE
       InputStream1.Close
       
        ListView1.AddTwoLinesAndBitmap("test","teste",Bitmap1)
     
   
     
   End If

   c.Close



Log(Buffer.Length) = 11717



** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
11717
main_readblob (java line: 414)
java.lang.RuntimeException: Error loading bitmap.
at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:521)
at com.manoelneto.texana.main._readblob(main.java:414)
at com.manoelneto.texana.main._activity_create(main.java:341)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at com.manoelneto.texana.main.afterFirstLayout(main.java:102)
at com.manoelneto.texana.main.access$000(main.java:17)
at com.manoelneto.texana.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
 

manoel vieira

Member
Licensed User
Longtime User
Erel, the size of the image file is 30K,
I did the following test, if I burn the image of B4A using the code below and then try to recover works perfect ... but if I try to read the same image file already recorded by another application is not reading.

B4X:
    c = s.ExecQuery2("SELECT myPhoto FROM catalog WHERE pk_produto = ?", Array As String("1"))
    c.Position = 0
    
    Dim Buffer() As Byte
        
    If Buffer <> Null Then
            Dim InputStream1 As InputStream
            Dim OutputStream1 As OutputStream
            OutputStream1.InitializeToBytesArray(1000)
          
            InputStream1 = File.OpenInput(File.DirAssets, "myPhoto.jpg")
      
          
            File.Copy2(InputStream1, OutputStream1)
          
          
            Buffer = OutputStream1.ToBytesArray
          
            'write the image to the database
            'write the image to the database
            s.ExecNonQuery2("INSERT INTO catalog VALUES( NULL, 'Paint Palette', ?,11,11)", Array As Object(OutputStream1.ToBytesArray))


  
      
     End If
  
     c.Close

reading image recorded by B4A......

B4X:
c = s.ExecQuery("SELECT myPhoto FROM catalog WHERE pk_produto = 1")
c.Position = 0Dim Buffer() As Byte 'declare an empty byte array Buffer = c.GetBlob("myPhoto")

If Buffer <> NullAnd Buffer.Length >1Then


Dim InputStream1 AsInputStream
InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)

Dim Bitmap1 AsBitmap
Bitmap1.Initialize2(InputStream1) 'ERROR HERE InputStream1.Close

ListView1.AddTwoLinesAndBitmap("test","teste",Bitmap1)


EndIf

c.Close

all this works, but if done by propro code within the B4A!

When I try to recover something that was already recorded then the error happens, I suspect that is the standard of incompatible file, but do not really know, talves there a way that I do reading this blob field in another way, really do not understand the occurs.

Erel, I'll keep trying, if you can move, I'll post here ....
 

manoel vieira

Member
Licensed User
Longtime User
Erel, following file with small SQLite database project containing data stored in the ZIP file.
Here is a photo of an example that is working on access to data using PHP.
 

Attachments

  • Texana_Botas.zip
    511.5 KB · Views: 221

manoel vieira

Member
Licensed User
Longtime User
Here is a photo of an example that is working on access to data stored in the database (texana.db) SQLite using PHP.
 

Attachments

  • erel_image_save_in_aplication_php_in_database_texana_db_ok.zip
    110.2 KB · Views: 212

Erel

B4X founder
Staff member
Licensed User
Longtime User
I saved the blob to a file and checked it with Notepad++:

SS-2016-05-26_11.51.40.png


I don't know what you are doing in your PHP code, however the data is stored as a base64 string with a prefix.

This code extracts it properly:
B4X:
Dim str As String = c.GetString("item")
Dim su As StringUtils
Dim buffer() As Byte = su.DecodeBase64(str.SubString(4))
 

manoel vieira

Member
Licensed User
Longtime User
Erel, perfect! now everything works, nothing like terms help those who really understand the subject. Thanks to your help I can continue my project, the fact that the blob field is recorded as a string is because I am using an interface on the Web developed in PHP using a framework. SCRIPTCASE use in my external aplicalçoes, so my application that storing the images are made by software in PHP.
Now everything is working
:) thank you friend.... :)
 

DrownedBat

Member
Licensed User
Longtime User
Is there any way to circumnavigate the 1MB limit on blobs? If I had a 5mb blob saved in a database, could I create a loop that extracted 1MB at a time into the buffer and then initialize a bitmap using an inputstream from the buffer?
 

DonManfred

Expert
Licensed User
Longtime User
Status
Not open for further replies.
Top