查看网络图片

1.运行效果图


2.训练目标

      输入网络图片的地址,点击浏览按钮可以显示网络中的图片。


3. 开发步骤

   MainActivity.java代码:

package org.bzu.imagebrowser;

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

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.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
	Button button;
	ImageView imageView;
	EditText ed;
	private static final int MSG_SUCCESS = 0;// 获取图片成功的标识
	private static final int MSG_FAILURE = 1;// 获取图片失败的标识

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findId();
	}

	public void findId() {
		button = (Button) findViewById(R.id.btnView);
		imageView = (ImageView) findViewById(R.id.ivImage);
		ed = (EditText) findViewById(R.id.edt);
	}

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

	private Handler handler = new Handler(){
	
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_SUCCESS:
				Bitmap bit = (Bitmap) msg.obj;
				imageView.setImageBitmap(bit);
				break;
			default:
				break;
			}
		};

	};

	public void viewImage(View view) {

		final String path = ed.getText().toString();//获取输入的图片路径
		if (TextUtils.isEmpty(path)) {
			Toast.makeText(MainActivity.this, " 路径不为空", Toast.LENGTH_SHORT)
					.show();
		} else {
			new Thread() {
				public void run() {
					URL url;
					try {
						url = new URL(path);//发出请求
						HttpURLConnection connection = (HttpURLConnection) url
								.openConnection();
						connection.setRequestMethod("GET");//设置请求方式这里的方式必须为大写
						connection.setConnectTimeout(5000);//设置超时的时间
						int code = connection.getResponseCode();//获得状态码
						if (code == 200) {
							InputStream is = connection.getInputStream();
							Bitmap bitmap = BitmapFactory.decodeStream(is);//写入一个bitmap流
							Message m = new Message();
							m.what = MSG_SUCCESS;
							m.obj = bitmap;
							handler.sendMessage(m);
						} else {
							Toast.makeText(MainActivity.this, "打开失败",
									Toast.LENGTH_SHORT).show();
						}

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

				};
			}.start();
		}

	}
}
  

 activity_main.xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/ivImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1" />

    <EditText
        android:id="@+id/edt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:text="http://cdn.duitang.com/uploads/item/201112/17/20111217144319_WW5Mj.thumb.600_0.jpg"/>

    <Button
        android:id="@+id/btnView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:onClick="viewImage"
        android:text="浏览" />

</LinearLayout>













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值