开发者

Load an image from url on button click

How to load an image from url on a button click and that image will be showed on activity itself?

I try the following code but it shows the system error like java.net.UnknownHostException Host is on resolved.

package com.v3.thread.fetchImage;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.HttpException;
import org.apache.http.conn.HttpHostConnectException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainThreadActivity extends Activity {
    ImageView img_downloaded;
    Button btn_download;
    String fileurl = "http://variable3.com/files/images/email-sig.jpg";

Bitmap bmImg;
private ProgressDialog dialog;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //new MainThreadActivity().onPreExecute();

    img_downloaded = (ImageView) findViewById(R.id.imageView1);
    btn_download = (Button) findViewById(R.id.btnLoad);

    btn_download.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            try {
                downloadfile(fileurl);
            } catch (HttpHostConnectException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (HttpException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
}


public void AbstractProgressTask() {
    dialog = new ProgressDialog(this);
}

protected void onPreExecute() {new MainThreadActivity().onPreExecute();
    this.dialog.setMessage("Loading. Please wait...");
    this.dialog.show();
}

 /**
 * method is called after the work is done.
 *
 * @param success result of #doInBackground method.
 */

protected void onPostExecute(final Boolean success) {
    if (dialog.isShowing()) {
        dialog.dismiss();
    }
}
// automatically done on worker thread (separate from UI thread)
protected Boolean doInBackground(final String... args) {

  // here is your code
  return true;
}

void downloadfile(String fileurl) throws HttpException,HttpHostConnectException {
    URL myFileUrl = null;
    try {
        myFileUrl = new URL(fileurl);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        int length = conn.getContentLength();
        if(length>0)
        {
            InputStream is = conn.getInputStream();
            bmImg = BitmapFactory.decodeStream(is);
            img_download.setImageBitmap(bmImg);
        }
        else
        {
            InputStream is = conn.getInputStream();
            bmImg = BitmapFactory.decodeStream(is);
        }

    } 
    catch(开发者_StackOverflow社区IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}

        {
            int[] bitmapData =new int[length];
            byte[] bitmapData2 =new byte[length];
            InputStream is = conn.getInputStream();
            bmImg = BitmapFactory.decodeStream(is);
        }

    } 
    catch(IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}

}


You should launch AsyncTask with ProgressDialog bounded(see an example) on button click. This is made by setting onClickListener on the button. In that AsyncTask you should download an image(see here). After you have dowloaded this, just call a method on your activity from onPostExecute() to show the image.


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.x);

    Button butt = (Button) this.findViewById(R.id.Button01);
    imageView =(ImageView) findViewById(R.id.ImageView01);
    Bitmap image;

    butt.setOnClickListener(new OnClickListener(){
        public void onClick(View v)  {
            URL url = null;
            try {
                url = new URL("http://lh5.ggpht.com/_hepKlJWopDg/TB-_WXikaYI/AAAAAAAAElI/715k4NvBM4w/s144-c/IMG_0075.JPG");
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            try {
                image = BitmapFactory.decodeStream(url.openStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
            imageView.setImageBitmap(image);
        }
        });
}

Dont forget to include the following permission if you download image from net.

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜