安卓系统:图像未从服务器下载


Android: Images not downloading from server

我在服务器上上传了一些图像,这些图像将通过传递url下载到我的android应用程序。我已经写了一个显示图像的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 />';
 ?>     

您似乎希望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