Android通过URL查看图片

本文介绍如何在Android应用中使用HttpURLConnection从URL加载图片。首先,通过URL实例化HttpURLConnection并设置请求参数,然后获取InputStream并转换为Bitmap显示在ImageView上。别忘了在AndroidManifest.xml中添加INTERNET权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      标准Java接口(java.net) ----HttpURLConnection,可以实现简单的基于URL请求、响应功能;任何一种接口,无外乎四个基本功能:访问网页、下载图片或文件、上传文件.本文示范的是访问网页和下载图片。
运行如下:

   ---------HttpURLConnection继承自URLConnection类,用它可以发送和接口任何类型和长度的数据,且预先不用知道数据流的长度,可以设置请求方式get或post、超时时间。
      1.使用HttpURLConnection的步骤是先实例化一个URL对象,通过URL的openConnection实例化HttpURLConnection对象。然后设置参数。
          注意此时并没有发生连接。真正发生连接是在获得流时即conn.getInputStream
      2.在android中显示网络上的图片,需要先根据url找到图片地址,然后把该图片转化成java的InputStream,然后把该 InputStream流转化成BitMap,
         BitMap可以直接显示在android中的ImageView里。
      3.AndroidManifest.xml里记得加访问网络的权限:
<uses-permission android:name="android.permission.INTERNET"/>
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private EditText etUrl;
	private ImageView ivImageShow;
	public static final int SHOWIMAGE=1;
	//主线程中创建消息处理器
	private Handler handler=new Handler(){
			public void handleMessage(Message msg){
				//对接收到的新消息进行处理
				switch(msg.what){
				case SHOWIMAGE:
					Bitmap bitmap=(Bitmap) msg.obj;
					ivImageShow.setImageBitmap(bitmap);
					break;
					default:
						break;
				}
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initViews();
	}
	private void initViews() {
		// TODO Auto-generated method stub
		etUrl=(EditText) findViewById(R.id.etUrl);
		ivImageShow=(ImageView) findViewById(R.id.ivImage);				
	}
	
	public void ShowImage(View v){
		final String path=etUrl.getText().toString();
		if(TextUtils.isEmpty(path)){
			Toast.makeText(this, "路径不能为空", Toast.LENGTH_LONG).show();
		}else{
			new Thread(){
			public void run(){
				try {
					//连接服务器,获取图片
					URL url=new URL(path);
					//发出http请求
				    HttpURLConnection connection=(HttpURLConnection) url.openConnection();
				    connection.setRequestMethod("GET");
				    //连接超时的时间
				    connection.setConnectTimeout(5000);
				    int responseCode=connection.getResponseCode();
				    if(responseCode==200){
				    	InputStream inputStream=connection.getInputStream();
				    	Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
				    	//ivImageShow.setImageBitmap(bitmap);
				    	//告诉主线程,帮我更新界面,内容是bitmap
				    	Message message=new Message();
				    	message.what=SHOWIMAGE;
				    	message.obj=bitmap;
				    	handler.sendMessage(message);
				    	}else{
				    		Toast.makeText(MainActivity.this, "图片显示失败!", Toast.LENGTH_LONG).show();
				    	}
				    } catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					}
				};
			}.start();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值