0

My MYSQL db contains 5 rows

id, name, username, password, email, image

The image type is BLOB.

My PHP codes below to get JSON data from MYSQL.

while($row = mysqli_fetch_array($res)){
     array_push($result,
         array('id'=>$row[0],
               'name'=>$row[1],
               'username'=>$row[2],
               'password'=>$row[3],
               'email'=>$row[4],
               'image'=>$row[5])
               ));
}

echo json_encode(array("result"=>$result));

My application will load longer than expected to parse the base64 image string from the returned JSON.

So what's the best way to store and retrieve image file efficiently?

2
  • 1
    one suggestion is to store imagepath and not image in your db , and show images in android from that imagepath you received via json Commented Oct 5, 2015 at 6:26
  • The simple answer is it's completely inefficient. and the only way to make it efficient is not to store images as blobs. Commented Oct 5, 2015 at 7:17

2 Answers 2

3

I had a similar problem. So I avoided storing images in MySql. I am storing them in the server as image files and using picasso to show the images. Its super easy to transform and use.

Sign up to request clarification or add additional context in comments.

Comments

0

Assuming your Above code is correct and you are getting base64 as image you can simply do this

while($row = mysqli_fetch_array($res)){
     array_push($result,
         array('id'=>$row[0],
               'name'=>$row[1],
               'username'=>$row[2],
               'password'=>$row[3],
               'email'=>$row[4],
               'image'=>$row[5])
               ));
}

echo json_encode(array("result"=>$result));

on Android side you can do some thing like this to convert it to image.

byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

Convert base64 string to Image in Java taken as reference

7 Comments

I've tried this, I got a textview to display JSON data, it show null when use this method
@cwfei : originally you were using reference to the image like &row[5] and not to value itself i.e $row[5] try this approach again
That's my mistake but I've correct it, without base64_decode i get something like "image":"\/9j\/4AAQSkZJRASJDASDJA\/........", a very long string
thats the actual base 64 converted image all you need to do is send the data as it is to client i.e android device and decode it on client end.
@cwfei: All you have to do is send the base64 string to client i.e android and decode it there, i have shared a sample on how to do it, there are other few ways to do that too.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.