从网址上的按钮点击加载图像 [英] Load an image from url on button click

查看:150
本文介绍了从网址上的按钮点击加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从网址上的按钮,点击加载图像,该图像将在活动本身显示?

我尝试以下code,但它显示了类似的java.net.UnknownHostException主机的系统误差的解决。

 包com.v3.thread.fetchImage;进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.net.HttpURLConnection中;
进口java.net.MalformedURLException;
进口的java.net.URL;进口org.apache.http.HttpException;
进口org.apache.http.conn.HttpHostConnectException;进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.widget.Button;
进口android.widget.ImageView;公共类MainThreadActivity延伸活动{
    ImageView的img_downloaded;
    按钮btn_download;
    字符串fileurl =htt​​p://variable3.com/files/images/email-sig.jpg;位图bmImg;
私人ProgressDialog对话框;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    //新MainThreadActivity()在preExecute()。    img_downloaded =(ImageView的)findViewById(R.id.imageView1);
    btn_download =(按钮)findViewById(R.id.btnLoad);    btn_download.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(查看为arg0){
            尝试{
                downloadfile(fileurl);
            }赶上(HttpHostConnectException E){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(HttpException E){
                // TODO自动生成catch块
                e.printStackTrace();
            }
        }
    });
}
公共无效AbstractProgressTask(){
    对话框=新ProgressDialog(本);
}在preExecute)保护无效(){新MainThreadActivity()在preExecute(。
    this.dialog.setMessage(Loading请稍候......);
    this.dialog.show();
} / **
 *工作完成后,方法被调用。
 *
 * #doInBackground方法@param成功的结果。
 * /保护无效onPostExecute(最终布尔成功){
    如果(dialog.isShowing()){
        dialog.dismiss();
    }
}
//在工作线程自动完成(从UI线程分开)
保护布尔doInBackground(最终字符串参数... args){  //这里是你的code
  返回true;
}无效downloadfile(字符串fileurl)抛出HttpException,HttpHostConnectException {
    URL myFileUrl = NULL;
    尝试{
        myFileUrl =新的URL(fileurl);
    }赶上(MalformedURLException的E){
        // TODO自动生成catch块
        e.printStackTrace();
    }
    尝试{
        HttpURLConnection的康恩=(HttpURLConnection类)myFileUrl.openConnection();
        conn.setDoInput(真);
        conn.connect();
        INT长度= conn.getContentLength();
        如果(长度大于0)
        {
            InputStream为= conn.getInputStream();
            bmImg = BitmapFactory.de codeStream(是);
            img_download.setImageBitmap(bmImg);
        }
        其他
        {
            InputStream为= conn.getInputStream();
            bmImg = BitmapFactory.de codeStream(是);
        }    }
    赶上(IOException异常E)
    {
        // TODO自动生成catch块
        e.printStackTrace();
    }}
}        {
            INT [] =的bitmapData新INT [长度]
            字节[] = bitmapData2新的字节[长度]
            InputStream为= conn.getInputStream();
            bmImg = BitmapFactory.de codeStream(是);
        }    }
    赶上(IOException异常E)
    {
        // TODO自动生成catch块
        e.printStackTrace();
    }}
}}


解决方案

 公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.x);    按钮对接=(按钮)this.findViewById(R.id.Button01);
    ImageView的=(ImageView的)findViewById(R.id.ImageView01);
    位图图像;    butt.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            网址URL = NULL;
            尝试{
                URL =新的URL(http://lh5.ggpht.com/_hepKlJWopDg/TB-_WXikaYI/AAAAAAAAElI/715k4NvBM4w/s144-c/IMG_0075.JPG);
            }赶上(MalformedURLException的E){
                e.printStackTrace();
            }
            尝试{
                图像= BitmapFactory.de codeStream(url.openStream());
            }赶上(IOException异常五){
                e.printStackTrace();
            }
            imageView.setImageBitmap(图片);
        }
        });
}

不要忘了,如果你从网上下载图像,包括以下权限。

 <使用许可权的android:NAME =android.permission.INTERNET对>< /使用许可权>

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

}
}

}

解决方案

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>

这篇关于从网址上的按钮点击加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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