【问题标题】:Android: Images not downloading from serverAndroid:图像未从服务器下载
【发布时间】:2015-09-10 10:14:56
【问题描述】:

我在我的服务器上上传了几张图片,这些图片将通过url 下载到我的安卓应用程序中。我写了一个显示图像的php 文件。我将URL 传递给我的 android 应用程序,如下所示:

'http://myURL/getImage.php?image=logo.png'.

当我将URL 直接复制粘贴到浏览器中时,图像会正确显示。 但是,图像文件没有在我的 android 应用程序上下载。我知道 android 代码是正确的,因为当我提供任何其他随机图像 URL 时,图像正在正确下载。我是否必须在我的 php 文件中提供其他内容才能使图像“可下载”。

Image.php

<?php

  echo '<img src="Images/'.$_REQUEST['image'].'"/><br />';

 ?>     

【问题讨论】:

  • 你的 android(java) 代码在哪里?
  • 只是......它不是图像......它是带有 img 标签的 HTML 页面......您需要发送二进制图像而不是 HTML 页面......不,我不知道如何做到这一点,因为我不喜欢 PHP,但使用 google(30 秒)应该很容易找到... 编辑: 你也可以将链接更改为 http://myURL/Images/logo.png 和这个链接应该返回图像二进制本身(应该很明显)
  • 感谢 HTML 的建议,但我必须使用 php。

标签: php android image server


【解决方案1】:

您似乎希望 PHP 直接输出图像。相反,您的代码正在生成带有图像的 HTML。尽管您的浏览器显示的结果相似,但底层内容不同。

你真正需要的可能是这样的:

<?php

$filepath = 'Images/'.$_REQUEST['image'];
$filename = basename($filepath);
$ctype = 'image/jpeg'; // assuming it is a .jpg file

if (is_file($filepath)) {
    header('Content-Type: '.$ctype);
    header('Content-Length: ' . filesize($filepath));
    header('Content-Disposition: attachment; filename="'.$fileName.'"');
    echo file_get_contents($file);
    exit();
} else {
    header("HTTP/1.0 404 Not Found");
    // you may add some other message here
    exit();
}

这很容易受到危险的$_REQUEST['image'] 输入的影响。只是以某种方式过滤它。此外,您必须为图像文件生成正确的$ctype

【讨论】:

    猜你喜欢
    • 2012-09-22
    • 2016-01-21
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多