关于android的网络访问——实现网络图片查看器

       代码很简单,知识实现一个简单的网络图片查看器的功能,具体实现过程的照片我就不贴了,具体实现的代码如下:

   

             一。布局代码:

                 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/imagePath"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="http://www.faxingw.cn/userimg/201110/30147.jpg">
    </EditText>

    <Button
        android:id="@+id/check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查看图片" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

   二。功能实现代码:

  

package org.jrue.netImageCehecker.activity;

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

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends Activity {

	private EditText imagePath;
	private Button btn_check;
	private ImageView iv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		inti();
	}

	private void inti() {
		imagePath = (EditText)findViewById(R.id.imagePath);
		btn_check = (Button)findViewById(R.id.check);
		iv = (ImageView)findViewById(R.id.imageView);
		
		btn_check.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new Thread(runnable).start();
			}
		});
	}

	private Runnable runnable = new Runnable(){

		@Override
		public void run() {
			String path = imagePath.getText().toString().trim();
			byte[] data = null;
			try {
				URL url = new URL(path);
				HttpURLConnection conn = (HttpURLConnection) url.openConnection();
				conn.setConnectTimeout(5000);
				conn.setRequestMethod("GET");
				if(conn.getResponseCode() == 200){
					InputStream is = conn.getInputStream();
					ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
					byte[] buffer = new byte[1024];
					int len = 0;
					while((len = is.read(buffer))!= -1){
						outputStream.write(buffer, 0, len);
					}
					outputStream.close();
					data = outputStream.toByteArray();
				}
				
				Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
				Looper.prepare();
				Message msg = new Message();
				msg.what = 0;
				msg.obj = bitmap;
				handler.sendMessage(msg);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		
	};
	private Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			if(msg.what == 0){
				iv.setImageBitmap((Bitmap) msg.obj);
			}
		};
	};

}

很简单,而且代码放在一起比较冗杂,感兴趣的可以细分下来,定义service包,utils包之类的,以实现代码清晰化。这是很好的编码风格。这里只为展示需要,并没有做多大的处理。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值