douqulv6059
2015-10-05 06:17
浏览 46

如何有效地通过JSON从MYSQL服务器获取BLOB映像文件

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?

图片转代码服务由CSDN问答提供 功能建议

我的MYSQL数据库包含5行

  id,name, 用户名,密码,电子邮件,图像
   
 
 

图像类型为BLOB。

下面我的PHP代码从MYSQL获取JSON数据。

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

我的应用程序将加载比预期更长的时间来从返回的JSON中解析base64图像字符串。

那么存储和存储的最佳方法是什么 有效地检索图像文件?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douyue5856 2015-10-05 06:31
    已采纳

    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.

    打赏 评论
  • duanlu6114 2015-10-05 06:29

    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

    打赏 评论

相关推荐 更多相似问题