php - 从远程 mysql 数据库检索图像到 android

标签 php android mysql json http

如何获取服务器上mysql中存储的图片?我想根据用户输入的值获取这些图像(意味着动态查询)。我想使用 json 将图像从 php 发送到 android。我使用静态查询在网络浏览器中成功显示了该图像,但是当我在 android 应用程序中看到 php 的响应时,它显示了一些 html 代码而不是图像。请帮助我解析图像 json 响应并显示该图像。 以下是将图像发送到 android 的 php 代码。

 while($post = mysql_fetch_object($data))
 {
    $posts[] = $post;

 }
         //header("Content-type: image/jpeg");

         echo '{"pprs":'.json_encode($posts).'}'; 

现在我收到回复:{"pprs":[{"pprs":null},{"pprs":null}]}

请帮助我。谢谢。

最佳答案

如果要使用 JSON,则需要将图像转换为字符串:

    $posts[] = base64_encode($post);

在 Android 端:

String base64image = jsonobject.getString("pprs");
byte[] rawImage = Base64.decode(base64image, Base64.DEFAULT);
Bitmap bmp = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length);
ImageView imageview = (ImageView) findViewById(R.id.imgPicture);
imageview.setImageBitmap(bmp);

关于php - 从远程 mysql 数据库检索图像到 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12647224/

相关文章:

php - 在 symfony 上使用 url_for 和 link_to 确定协议(protocol)

PHP 包含 - 无法打开流 : No such file

android - 支持android中的所有屏幕尺寸吗?

java - 计算正则表达式匹配数

mysql - HIVE UNION ALL with 子句

php - 如何从递归函数返回数组

php - 在 PHP 中使用 str_replace() 将\n 替换为 ''

java - Android 应用内结算 - 部分卡不被接受

php - 将 fetch all 数组拆分为单独的数组

php - 无法在 Heroku 上访问 AWS RDS MySQL + PHP