Android中实现输入图片地址浏览图片操作

其中这两个java文件 请参考 上一篇 文章

 

public class HttpUtils {

	public static String sendGet(String path) {
		String content = null;
		try {
			// 设置访问url
			URL url = new URL(path);
			// 打开请求
			HttpURLConnection httpURLConnection = (HttpURLConnection) url
					.openConnection();
			// 设置请求的信息
			httpURLConnection.setRequestMethod("GET");
			// 设置请求是否超时时间
			httpURLConnection.setConnectTimeout(5000);
			// 判断服务器是否响应成功
			if (httpURLConnection.getResponseCode() == 200) {
				// 获取响应的输入流对象
				InputStream is = httpURLConnection.getInputStream();
				byte data[] = StreamTools.isToData(is);
				// 把转换成字符串
				content = new String(data);
				// 内容编码方式
				if (content.contains("gb2312")) {
					content = new String(data, "gb2312");
				}

			}
			// 断开连接
			httpURLConnection.disconnect();

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return content;
	}

	public static Bitmap sendGets(String path) {
		Bitmap bitmap = null;

		try {
			URL url = new URL(path);

			// 打开请求
			HttpURLConnection httpURLConnection = (HttpURLConnection) url
					.openConnection();
			// 设置请求的信息
			httpURLConnection.setRequestMethod("GET");
			// 设置请求是否超时时间
			httpURLConnection.setConnectTimeout(5000);
			// 判断服务器是否响应成功
			if (httpURLConnection.getResponseCode() == 200) {
				// 获取响应的输入流对象
				InputStream is = httpURLConnection.getInputStream();
				System.out.println("-----" + is);
				// 直接把这个is流转换成Bitmap对象
				bitmap = BitmapFactory.decodeStream(is);
			}
			// 断开连接
			httpURLConnection.disconnect();
		} catch (MalformedURLException e) {

			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}

		return bitmap;

	}

}


 

 

实现其功能MainActivity类:

public class MainActivity extends Activity {

	// 声明控件
	public EditText et_url;
	public ImageView iv_ie;

	// 网络操作类
	public NetWorkUtils netWorkUtils;

	private Handler handler;
	public Bitmap bm;
	public static final int TEXT = 1;

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

		// 获取et_url对象
		et_url = (EditText) findViewById(R.id.et_url);
		iv_ie = (ImageView) findViewById(R.id.iv_ie);
		// 实例化
		netWorkUtils = new NetWorkUtils(this);

		// 实例化这个处理者
		handler = new Handler() {
			
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case TEXT:
					iv_ie.setImageBitmap(bm);// 设置显示的文本
					break;
				default:
					break;
				}
			}
		};
	}

	@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;
	}
	
	public void sendHttp(View v){
		
		//设置网络
		boolean flag = netWorkUtils.setActiveNetWork();
		if(flag){
			//获取路径的值
			final String path = et_url.getText().toString();
			
			if(TextUtils.isEmpty(path)){
				Toast.makeText(MainActivity.this, "访问的url地址不能为空", Toast.LENGTH_LONG).show();
			}else{
				//子 线程
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						bm = HttpUtils.sendGets(path);
						handler.sendEmptyMessage(TEXT);
						
					}
				}).start();
			}
		}
	}

}


==========================

布局:

<EditText
        android:id="@+id/et_url"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:text="@string/url_text" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/et_url"
        android:onClick="sendHttp"
        android:text="@string/btn_text" />

    <ImageView
        android:id="@+id/iv_ie"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/button1"
        android:src="@drawable/ic_launcher" />


String文件

<string name="url_text">http://172.22.64.20:8080/test/images/0.jpg</string>
    <string name="btn_text">浏览</string>

 

记得添加访问网络的权限 ,请参考上一篇文章

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值