Android Question get image from php

Endien

Member
Licensed User
Longtime User
Hi all,

i'm trying to get image from php with android. I use code example from https://www.b4x.com/android/forum/threads/httputils2-web-services-are-now-even-simpler.18992/

This is my php code :

PHP:
while($result=mysqli_fetch_array($sth)) {

    echo '<img src="data:image/jpeg;base64,' . base64_encode($result['productImg']) . '"/><br>';
}

And this is the code in b4a :

B4X:
Sub HandleMainPage (Job As HttpJob)
    imageLinks.Clear
    CallSub(ImageDownloader, "ClearCache")
    ProgressDialogHide
    If Job.Success = False Then
        ToastMessageShow("Error downloading main page.", True)
        Return
    End If
    ResetImagesBackground
    
    Dim m As Matcher = Regex.Matcher("([0-9a-zA-Z\+/=]{20,})", Job.GetString)
   
    Do While m.Find
        Log(m.Group(1))
        imageLinks.Add(m.Group(1))
    Loop
    DownloadImages
End Sub

first i use
B4X:
Dim m As Matcher = Regex.Matcher("([0-9a-zA-Z\+/=]{20,})", Job.GetString)
to get encoded base64 string from php.

and the error is arise in :
B4X:
imageLinks.Add(m.Group(1))

since m.group(1) result is still in base64 encoded string.

I think i should decode the m.group(1) regular string.
How to to that?

Or maybe i was thinking wrong? please correct me.
 

walterf25

Expert
Licensed User
Longtime User
Hi all,

i'm trying to get image from php with android. I use code example from https://www.b4x.com/android/forum/threads/httputils2-web-services-are-now-even-simpler.18992/

This is my php code :

PHP:
while($result=mysqli_fetch_array($sth)) {

    echo '<img src="data:image/jpeg;base64,' . base64_encode($result['productImg']) . '"/><br>';
}

And this is the code in b4a :

B4X:
Sub HandleMainPage (Job As HttpJob)
    imageLinks.Clear
    CallSub(ImageDownloader, "ClearCache")
    ProgressDialogHide
    If Job.Success = False Then
        ToastMessageShow("Error downloading main page.", True)
        Return
    End If
    ResetImagesBackground
   
    Dim m As Matcher = Regex.Matcher("([0-9a-zA-Z\+/=]{20,})", Job.GetString)
  
    Do While m.Find
        Log(m.Group(1))
        imageLinks.Add(m.Group(1))
    Loop
    DownloadImages
End Sub

first i use
B4X:
Dim m As Matcher = Regex.Matcher("([0-9a-zA-Z\+/=]{20,})", Job.GetString)
to get encoded base64 string from php.

and the error is arise in :
B4X:
imageLinks.Add(m.Group(1))

since m.group(1) result is still in base64 encoded string.

I think i should decode the m.group(1) regular string.
How to to that?

Or maybe i was thinking wrong? please correct me.
What is the error you get?
 
Upvote 0

Endien

Member
Licensed User
Longtime User
I use emulator since i use localhost (10.0.2.2)
And there were no error on emulator and on log there were error message :

B4X:
Error downloading image: /9j/4AAQSkZJRgABAQAAAQABAAD/4QBARXhpZgAATU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAqACAAQAAAABAAAC2qADAAQAAAABAAACagAAAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwYICAsJCAgKCAYHCQ0KCgsMDAwMBwkNDg0MDgsMDAz/2wBDAQICAgMCAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAJqAtoDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0t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ReRvI4ml3tJLJvd8NmvmLVtH0i9+ZdIjjZshipPI6dC1RW/wAPfCKQo09pdhwAoTzWMafRM7R+Ar1pcN4qjUbizgjmkL2ufVFronwD8d+KPFa6DH8DY9NufEd3HrjapJp8b2mlJo8LRTaTuQ+WovVJAi2h3Eq5BZjXi/7VHwk8C+G/2Q/BHirwZp+maTGdZl0wSXN2jav4utdwFtfRptDvE0ab5IgzrAZ403vkMfLLv4X+H7qQJFDcwwHGSrYPXNWD8IvDVriaLUdRMwO5UkdnSM9flUjC/hXP/q/jTo+v0u558Z3e4llLm4lwI55Y18tUbqEKjOeMHJ9cY4o+1e5rt1+D1pfOiLqZt4QSQzA8H3AXJpuofB20s4j5etQSt/1zk/8AiK56uT4uk7NFLGUpdTivtR9TR9q9zXV2vwSv9RiDw39l5Z5UksCfwIqvqPwh1Gx+Tz7OQ5AyrHJrL+zcVHVoqOJpSdkznPtPuaBIx5zkVvr8GvEDRbvItvL/AL28Z/nVST4daxa3Pl/YmkbOMq68/rU/Va38pvzQ/mKCzKB8wUe+Ka9+EPysPyrQuvAmsWoO/T5kCjJZivPtwazm0m9KMxs7sAf3FBqJUKkdHEOZPaQreU4GWiYkZ4IBqIwW24Exhj2LAPj/AAp0trc2yAyW8kX+8hqAzTLKo2tg9wjf4VLhOOvKCSlpzF+2uzCP3cnljHaPH9au2/i/WIWzDresxkf88tUl/oaxGJEn30Psyt/hSmcP95v/AB0ip55fyley/vHVWfxU8WaYf3Xi3xbDnoBqlxg/+P1vaf8AtJ/EDTo/3XjbxLxz+81Gcj8fnrzUlc/Kyn1yTx+dPVgi5JYn0BHNHMusQtFaM9Utv2vPiNA+4+J5Lj2nQ3H6M9a9p+2548TBmn0G8wesulxq2PTnPFeLrKJEOVkX8RRDIACD5iHPfBounsh+70PfbT9vDxJCB5+heH7nHXNjGFP/AI4a1dK/4KF6vaON3hXS1H/TvdLbf+gxV82seMYJXuSel

Message longer than Log limit (4000). Message was truncated.
 
Upvote 0

Endien

Member
Licensed User
Longtime User
Now i use 2 php file to load the image :

imageView.php

PHP:
<?php
$conn = mysql_connect("localhost", "blabla", "blabla");
mysql_select_db("myDb") or die(mysql_error());
if(isset($_GET['productId'])) {
    $sql = "SELECT productImg FROM product WHERE productId=" . $_GET['productId'];
    $result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error());
    $row = mysql_fetch_array($result);
    header("Content-type: image/jpg");
    echo $row["productImg"];
}
mysql_close($conn);
?>

And listImages.php

PHP:
<?php
$conn = mysql_connect("localhost", "blabla", "blabla");
mysql_select_db("myDb");
$sql = "SELECT productId FROM product ORDER BY productId DESC";
$result = mysql_query($sql);
?>
<HTML>
<HEAD>
    <TITLE>List BLOB Images</TITLE>
</HEAD>
<BODY>
<?php
while($row = mysql_fetch_array($result)) {
    ?>
    <img src="http://10.0.2.2/SI/imageView.php?kode=<?php echo $row["productId"]; ?>" /><br/>
<?php
}
mysql_close($conn);
?>
</BODY>
</HTML>

i have 6 images in my mySql database
When i run the android app, the image were load but sometime it load 2 images, sometime it load 4 images and then error and the app stopped.
the sequence of the images shown is random.

the error is on this line :
Dim bmp As Bitmap = Job.GetBitmap

and this is the error detail :

B4X:
java.lang.RuntimeException: Error loading bitmap.

    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:521)
    at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize(CanvasWrapper.java:499)
    at anywheresoftware.b4a.keywords.Common.LoadBitmap(Common.java:1125)
    at anywheresoftware.b4a.samples.httputils2.httpjob._getbitmap(httpjob.java:116)
    at anywheresoftware.b4a.samples.flickr.imagedownloader._jobdone(imagedownloader.java:222)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    at anywheresoftware.b4a.keywords.Common$5.run(Common.java:962)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)


    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)

is it any limitation of image size or image dimension for it load on imageView?
my all image size is under 100k.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
echo $row["productImg"];
this is base64 value right? You need to send the PICTURE not the base64 encoded string

Try
B4X:
echo base64_decode($row['productImg']);
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
is it any limitation of image size or image dimension for it load on imageView?
my all image size is under 100k.
The FILESIZE does NOT matter. The Picture dimension does!
The problem here is not the filesize... You are sending a base64 encoded string as payload. You need to first base64-decode the data and send the plain imagedata....
 
Upvote 0

Endien

Member
Licensed User
Longtime User
this is base64 value right? You need to send the PICTURE not the base64 encoded string

Try
B4X:
echo base64_decode($row['productImg']);

To be honest, i dont quite understand php yet.
But yes i think it's base64 value

I tried your suggestion
B4X:
echo base64_decode($row['productImg']);
[/QUOTE]

But still it give me error in this line :
B4X:
Dim bmp As Bitmap = Job.GetBitmap

with the java exception like above.
 
Upvote 0

Endien

Member
Licensed User
Longtime User
The FILESIZE does NOT matter. The Picture dimension does!
The problem here is not the filesize... You are sending a base64 encoded string as payload. You need to first base64-decode the data and send the plain imagedata....

Turn out the problem is because of the dimension. I tried to scale down the dimension and the app worked fine.
What is the max dimension from a picture to be load correctly in imageView?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Turn out the problem is because of the dimension. I tried to scale down the dimension and the app worked fine.
Then the image is stored as blob (not baseencoded)

What is the max dimension from a picture to be load correctly in imageView?
It depends on the free ram your app can use...

What dimension do the images have in pixels?
 
Upvote 0

Endien

Member
Licensed User
Longtime User
Then the image is stored as blob (not baseencoded)


It depends on the free ram your app can use...

What dimension do the images have in pixels?

Thanks for the answer DonManfred
I use 350 pixels horizontally. the vertical pixels adjust according to the aspect ratio
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
that should not be a problem. Are you sure it's not timing out or something?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I'd do it like this

upload image
add image to database as path & filename
copy from the temp dir to your end path as id-original_filename to prevent overwrites when duplicate filenames are there

then you can just pull in a list of images as csv, xml, json or your own format and download it as regular images without all the encoding and decoding
 
Upvote 0

Endien

Member
Licensed User
Longtime User
I'd do it like this

upload image
add image to database as path & filename
copy from the temp dir to your end path as id-original_filename to prevent overwrites when duplicate filenames are there

then you can just pull in a list of images as csv, xml, json or your own format and download it as regular images without all the encoding and decoding

Thanks for the suggestion, sorex.
Yes, we can do like that also.

Anyway, the app is loading the image just fine right now with my current method. But your suggestion is very appreciated, sorex :)
 
Upvote 0
Top