【问题标题】:how to refresh the ImageView after setting image from the server In my case its not updating the ImageView从服务器设置图像后如何刷新 ImageView 在我的情况下,它没有更新 ImageView
【发布时间】:2015-08-23 20:25:00
【问题描述】:

问题是 当我从图库中获取图片并将其设置为图像视图时, 然后它被设置了,但是当我关闭该活动并再次打开它时, 它显示的是上一张图片而不是更新的图片, 我的代码完美地将图像发送到服务器并将其保存到服务器, 也在MYSQL中。 但是当我尝试从特定的 URL 检索图像时,它不是 正在检索图像.....

当我刷新服务器上保存更新图像的 URL 时, 它像这样向我显示 imageView 上的更新图像。

这是我正在下载的图像类....

package com.tut.app;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.InputStream;



public class DownloadImageTask extends AsyncTask<String, Void, Bitmap>
{
    private ImageView imgView;

    public DownloadImageTask(ImageView imageView)
    {
        this.imgView = imageView;

    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e(" Error coming data ", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    @Override
    protected void onPostExecute(Bitmap result)
    {
        Log.e("result ", String.valueOf(result));
        this.imgView.setImageBitmap(result);
    }

}

这是我的 PHP 脚本,用于设置图像并将其保存到数据库中

<?php

    // connect to mysql database
    $con = mysql_connect("127.0.0.1","root","pass123");
    if (!$con){
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("TestDatabase", $con); // name of your database

    //check if "image" abd "CustomerID" is set

    if(isset($_POST["image"]) && isset($_POST["CustomerID"])) {
        $data = $_POST["image"];
        $CustomerID = $_POST["CustomerID"];
        $ImageName = $CustomerID.".jpg";
        $filePath = "images/".$ImageName; // path of the file to store
        echo "file ".$filePath;
        // check if file exits
        if (file_exists($filePath)) {
            unlink($filePath); // delete the old file
        }
        // create a new empty file
        $myfile = fopen($filePath, "w") or die("Unable to open file!");
        // add data to that file
        file_put_contents($filePath, base64_decode($data));

        // update the Customer table with new image name.
        mysql_query("UPDATE Customers SET imageName='$ImageName' WHERE id='$CustomerID'")
            or die('Could not save Image Name: ' . mysql_error());

    } else {
        echo 'not set';
    }
    // close the connection
    mysql_close($con);

?>

【问题讨论】:

    标签: php android mysql image


    【解决方案1】:

    要从互联网上下载图像,请使用这个库,它有很多功能并且效果最好: http://square.github.io/picasso/

    【讨论】:

    • 我的问题是没有下载图像,我的问题是在我关闭该活动后设置该图像,当我重新打开它时,它会显示更新后的图像......我希望你现在明白了跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-06
    • 2016-10-08
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    相关资源
    最近更新 更多