Android Question Save image to mysql

taylorw

Active Member
Licensed User
Hello every one, i trying to convert image to byte then to string, everything okay and done save.
But when i read from Mysql database i get error.

B4X:
ExecuteRemoteQuery("SELECT * FROM tblTest", GetImage)

B4X:
Sub ExecuteRemoteQuery(Query As String, JobName As String)
    
    Dim job As HttpJob
    job.Initialize(JobName, Me)
    job.PostString("http://www.mydomain.com/myphp.php", Query)
    
End Sub

B4X:
Sub JobDone(Job As HttpJob)
    Dim MySQL_Data As List
    Dim M As Map
    
    If Job.Success Then
        Dim res As String
        res = Job.GetString
        Log("Response from server: " & res)
        Dim parser As JSONParser
        parser.Initialize(res)
        
        Select Job.JobName                           
            Case GetImage
                MySQL_Data = parser.NextArray  'This line get error
                
                If MySQL_Data.Size > 0 Then
                    For I = 0 To MySQL_Data.Size -1
                        M = MySQL_Data.Get(I)
                        Dim s As String  = M.Get("Image")
                        Dim b () As Byte = s.GetBytes("UTF8")
                        
                        BytesToFile(File.DirInternal, "Download.png", b)
                        
                        IV.Bitmap = LoadBitmap(File.DirInternal, "Download.png")
                    Next
                End If
                
        End Select
    Else
        Log(Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
    
End Sub

This is my error.

B4X:
Response from server: <img src="data:image/jpeg;base64,"/>
main_jobdone (B4A line: 70)
MySQL_Data = parser.NextArray
java.lang.RuntimeException: JSON Array expected.
    at anywheresoftware.b4a.objects.collections.JSONParser.NextArray(JSONParser.java:62)
    at b4a.example.main._jobdone(main.java:550)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
    at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1144)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
 

Emme Developer

Well-Known Member
Licensed User
Longtime User
You should use Job.GetString. Your server answer is wrong, you need a json response.
By the way, i suggest to avoid to save image in database, instead use an url and place the image into the server
 
Upvote 0

taylorw

Active Member
Licensed User
Sorry i checked my php is wong, and i change it like below.

B4X:
$con = mysqli_connect($databasehost,$databaseusername,$databasepassword, $databasename) or die(mysqli_error($con));
mysqli_set_charset ($con , "utf8");
$query = file_get_contents("php://input");
$sth = mysqli_query($con, $query);

if (mysqli_errno($con)) {
   header("HTTP/1.1 500 Internal Server Error");
   echo $query.'\n';
   echo mysqli_error($con);
}
else
{
   $rows = array();
   while($r = mysqli_fetch_assoc($sth)) {
     $rows[] = $r;
   }
   $res = json_encode($rows);
    echo $res;
    mysqli_free_result($sth);
}
mysqli_close($con);
?>

Thanks for your suggest.I will Try It
 
Upvote 0
Top