0

目前我正在尝试下载存储在我的服务器目录中的图像。但我不知道为什么我的应用程序仍然显示该图像,即使该图像被另一个图像替换。

这意味着首先我上传image1,然后我可以加载image1。当 image1 被 image2 替换时,应用程序仍然显示 image1。

我不确定我的错误在哪里。是代码还是其他原因。需要一些帮助!

下面是我的代码:

button1.setOnClickListener(new OnClickListener() {
    public void onClick(View view) {
        new download().execute();
    }
});

class download extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... path) {
        String outPut = null;
        String s = "http://url/image/img_123.jpg";
        URL myFileUrl = null;
        try {
            myFileUrl = new URL(s);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        try {
            HttpURLConnection conn = (HttpURLConnection) myFileUrl
                    .openConnection();
            conn.setDoInput(true);
            conn.connect();
            int length = conn.getContentLength();
            int[] bitmapData = new int[length];
            byte[] bitmapData2 = new byte[length];
            InputStream is = conn.getInputStream();

            bm = BitmapFactory.decodeStream(is);

            outPut = "success";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return outPut;
    }

    protected void onPostExecute(String file_url) {
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                imageView1.setImageBitmap(bm);
            }
        });
    }
}

更新

button1.setOnClickListener(new OnClickListener() {
    public void onClick(View view) {
        // new download().execute();
        Drawable drawable = LoadImageFromWeb("http://url/image/img_123.jpg");
        imageView1.setImageDrawable(drawable);
    }
});

private Drawable LoadImageFromWeb(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    } catch (Exception e) {
        System.out.println("Exc="+e);
        return null;
    }
}
4

1 回答 1

0

这是一个帮助从 URL 下载图像文件并存储您的自定义位置的示例,您可以在下载后显示在屏幕上 http://getablogger.blogspot.in/2008/01/android-download-image-from-服务器和.html

更新:

我认为您需要直接从 url 加载图像。请参见此处 http://progrnotes.blogspot.in/2010/09/url.html

于 2012-09-27T07:28:07.500 回答