【问题标题】:Image doesn't update when re-downloaded by app, after changing it on the server在服务器上更改图像后,应用程序重新下载图像时不会更新
【发布时间】:2013-12-06 23:44:41
【问题描述】:

编辑: 它确实更新了,但延迟了几个小时..

另一个编辑;

它只发生在我的 nexus 4 上,我今天在我的 SGS1 上测试了它,它可以正常工作。 可能是什么问题?

你可以说它有点复杂,所以请仔细阅读:

我有一个将图像上传到网络的应用程序,(图像可以是任何东西,来自用户的设备)。

我现在为该结构添加了一个编辑功能,如果用户想更改旧图像,还可以在其中上传新图像。

确实有效。我通过浏览器对其进行了测试,服务器上的图像确实发生了变化。

但另一方面,在应用程序上,由于某种原因它没有(图像没有改变,即使服务器上的图像现在已经改变并且之前的图像不存在了。 )。

就像,它被保存在设备的缓存或其他东西中......但是:

我不知道这是怎么发生的,因为每次我调用它时下载图像的函数都会重新启动。

这是怎么回事?

如果有人想要我的ImageDownloader 功能(但我百分百确定问题不存在):

private Bitmap downloadBitmap(String url) {
            // initilize the default HTTP client object
            final DefaultHttpClient client = new DefaultHttpClient();

            // forming a HttoGet request
            final HttpGet getRequest = new HttpGet(url);
            try {

                HttpResponse response = client.execute(getRequest);

                // check 200 OK for success
                final int statusCode = response.getStatusLine().getStatusCode();

                if (statusCode != HttpStatus.SC_OK) {
                    Log.w("ImageDownloader", "Error " + statusCode
                            + " while retrieving bitmap from " + url);
                    return null;

                }

                final HttpEntity entity = response.getEntity();
                if (entity != null) {
                    InputStream inputStream = null;
                    try {
                        // getting contents from the stream
                        inputStream = entity.getContent();

                        // decoding stream data back into image Bitmap that
                        // android understands
                        final Bitmap bitmap = BitmapFactory
                                .decodeStream(inputStream);

                        return bitmap;
                    } finally {
                        if (inputStream != null) {
                            inputStream.close();
                        }
                        entity.consumeContent();
                    }
                }
            } catch (Exception e) {
                // You Could provide a more explicit error message for
                // IOException
                getRequest.abort();
                Log.e("ImageDownloader", "Something went wrong while"
                        + " retrieving bitmap from " + url + e.toString());
            }

            return null;
        }

还有我的Edit.php 文件更新数据,包括图像(它有效,经过测试。):

<?php 
   //1. create connection

   $connection=mysql_connect("localhost","lalala","lalala");/
   if(!$connection)
   {
       die("database connection failed:" . mysql_error());
   }

   $db=mysql_select_db("lalala",$connection);
   if(!db)
   {
              die("database connection failed:" . mysql_error());
   }
   mysql_query("SET NAMES 'utf8'",$connection);


    if ($_POST["lala"]!=null)
   {
    if ($_POST["lalala"]!=null && $_POST["lalala"]!=null)
   {

    $sql="UPDATE `lalala`.`lalal` SET `xxx` = '".$_POST[xxx]."', `xxx`='".$_POST[xxx]."', `xxx` = '".$_POST[xxx]."', `xxx`='".$_POST[xxx]."', `xxxt`='".$_POST[xxx]."', `xxx`= '".$_POST[xxx]."', `xxx` = '".$_POST[xxx]."'  WHERE `xxx`.`xx` = ".$_POST[xx].";";
        $result=mysql_query($sql,$connection);
        if(!$result)
        {
            echo "error.";
            //echo "('".mysql_insert_id($connection)."','".$_POST[title]."','".$_POST[snippet]."','".$_POST[cat]."','".$_POST[desc]."','".$_POST[lat]."','".$_POST[lon]."','".$_POST[pub]."','0','0','".$_POST[program]."','".$_POST[addr]."')\n";
            die("database connection failed:" . mysql_error());
        }
        else {
                if ($_POST[isnewimg]==1)
                {
                    if (unlink("upload/id".$_POST[id].".jpg"))
                    {
                    $target = "upload/";
                    $target = $target . basename( $_FILES['uploaded']['name'].$_POST[id].".jpg");

                    if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
                     {
                        echo "1";
                     }
                    else {echo "Img update failed".$_POST[id];}
                    }
                    else {echo "Img deletion failed".$_POST[id];}
                }
                else
                    echo "1"."w/o image. updated ".$_POST[id];
            }


   }
   }


else {
echo "2";
}

mysql_close($connection);
?> 

【问题讨论】:

  • 警告:您使用的是a deprecated database API,应该使用modern replacement。您也容易受到 SQL injection attacks 的影响,现代 API 可以让您更轻松地从 defend 中获取信息。并且请在输出到 HTML 时使用htmlspecialchars 以防止 XSS。
  • 你试过清除数据吗?图片可能被缓存了。
  • @JoeBirch 是的。我通过设备上的app info清除了数据和缓存。
  • 我对php了解不多,但出现这种情况的原因很可能是在服务器上。您的 php 服务器可能正在缓存图像。尝试清除您的服务器缓存。
  • @Tim 我该怎么做?正如我所说,如果我手动转到图像的 url,它会显示新的 ..

标签: php android image bitmap image-uploading


【解决方案1】:

如果服务器正在缓存图像,请尝试重置它以确认这是真正的问题。

在客户端:

如果您从 URL 加载图像,例如:

http://ejemplo.com/imagen.png

您可以为每个请求生成一个 id 并将其添加到 URL 中,如下所示:

http://ejemplo.com/imagen.png?request_id=5385683

我不知道你在哪个平台上工作,但这适用于浏览器。

【讨论】:

  • 这有什么帮助?我的意思是,在浏览器上,我看到了新图像,但应用程序仍然显示旧图像..
  • 哦,我该如何重置我的服务器?它是我在网上找到的免费托管服务器......我如何为每个请求生成一个 id?
  • 我的意思是,重新启动服务器,对不起。但如果它是免费托管,我怀疑你能做到。
  • 在图片的情况下,仅建议您尝试在每个新请求的 url 中添加查询。因此,应用程序可能会将其作为新的 url,这在浏览器中有效。
  • 最后,每次更新为什么不改文件名?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-19
  • 2017-04-28
  • 2015-11-14
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 2014-03-24
相关资源
最近更新 更多