如何下载远程服务器并显示一个图像ANDROID(关闭) [英] How to download ONE image from remote server and display in ANDROID (CLOSED)

查看:185
本文介绍了如何下载远程服务器并显示一个图像ANDROID(关闭)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

意思就是首先我上传此搜索,然后我可以加载此搜索。在此搜索是IMAGE2更换,应用程序仍然显示此搜索。

林不知道哪里是我的错误。难道它位于code或其他原因。需要一些帮助!

下面是我的code:

  button1.setOnClickListener(新OnClickListener(){
    公共无效的onClick(查看视图){
        新的下载()执行();
    }
});类下载扩展的AsyncTask<弦乐,太虚,字符串> {    @覆盖
    保护字符串doInBackground(字符串...路径){
        字符串输出= NULL;
        字符串s =HTTP://url/image/img_123.jpg
        URL myFileUrl = NULL;
        尝试{
            myFileUrl =新的URL(S);
        }赶上(MalformedURLException的E){
            e.printStackTrace();
        }        尝试{
            HttpURLConnection的康恩=(HttpURLConnection类)myFileUrl
                    .openConnection();
            conn.setDoInput(真);
            conn.connect();
            INT长度= conn.getContentLength();
            INT [] =的bitmapData新INT [长度]
            字节[] = bitmapData2新的字节[长度]
            InputStream为= conn.getInputStream();            BM = BitmapFactory.de codeStream(是);            输出=成功;
        }赶上(IOException异常五){
            e.printStackTrace();
        }
        返回输出;
    }    保护无效onPostExecute(字符串FILE_URL){
        //从后台线程更新界面
        runOnUiThread(新的Runnable(){
            公共无效的run(){
                imageView1.setImageBitmap(BM);
            }
        });
    }
}

更新

  button1.setOnClickListener(新OnClickListener(){
    公共无效的onClick(查看视图){
        //新的下载()执行();
        可绘制可绘制= LoadImageFromWeb(HTTP://url/image/img_123.jpg);
        imageView1.setImageDrawable(绘制);
    }
});私人绘制对象LoadImageFromWeb(字符串URL){
    尝试{
        InputStream为=(InputStream的)新的URL(网址).getContent();
        可绘制D = Drawable.createFromStream(是,SRC名);
        返回D组;
    }赶上(例外五){
        的System.out.println(EXC =+ e)条;
        返回null;
    }
}


解决方案

下面是一个例子,这有助于从下载网址图像文件,存储在您的自定义位置,通过它可以下载后在屏幕上显示
http://getablogger.blogspot.in/2008/01/android-download-image-from-server-and.html

更新:

我认为你需要从这里url..see加载direcly形象
http://progrnotes.blogspot.in/2010/09/url.html

Currently I'm trying to download an image which is stored in my server directory. But I don't know why my app still display that image even though that image is replace with another image.

Meaning that firstly I upload image1, then I can load image1. when image1 is replace by image2, the app still show image1.

Im not sure where is my error. Does it lies in the code or other reason. Needed some help!

Below is my code:

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);
            }
        });
    }
}

UPDATE

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;
    }
}

解决方案

Here is one example which help to download image file from URL and it store your custom location, by which you can display on screen after download http://getablogger.blogspot.in/2008/01/android-download-image-from-server-and.html

Update:

I think you need to load image direcly from url..see here http://progrnotes.blogspot.in/2010/09/url.html

这篇关于如何下载远程服务器并显示一个图像ANDROID(关闭)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆