android网络图片查看器

可以先启动一个tomcat服务器,在网络上放一张图片

布局

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

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/path"/>
    
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:id="@+id/imagepath"
        android:text="@string/realpath"
        android:inputType="text"/>
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:id="@+id/button"
        android:text="@string/button"
        />
    
    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:id="@+id/image"
        android:contentDescription="@string/description"
        />
    
</LinearLayout>
mainactivity
package cn.wonders.image;

import cn.wonders.service.ImageService;
import android.support.v7.app.ActionBarActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
    private EditText et;
    private ImageView ev;
    private Bitmap bitmap;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_main);
//		final EditText et = (EditText) this.findViewById(R.id.imagepath);
		et = (EditText) this.findViewById(R.id.imagepath);
		Button button = (Button) this.findViewById(R.id.button);
//		final ImageView ev = (ImageView) this.findViewById(R.id.image);
		ev = (ImageView) this.findViewById(R.id.image);
		
		button.setOnClickListener(new ButtonClickListener());
//		button.setOnClickListener(new View.OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				// 使用匿名内部类,隐式调用外部变量,外部变量需要final修饰。
//				String path = et.getText().toString();
//				byte[] data;
//				try {
//					data = ImageService.getImage(path);
//					Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
//					ev.setImageBitmap(bitmap);
//				} catch (Exception e) {
//					e.printStackTrace();
//					Toast.makeText(getApplicationContext(), R.string.error, Toast.LENGTH_LONG).show();
//				}
//				//生成位图对象
//				//显示图片
//			}
//		});
		
//		if (savedInstanceState == null) {
//			getSupportFragmentManager().beginTransaction()
//					.add(R.id.container, new PlaceholderFragment()).commit();
//		}
	}

	Handler handler = new Handler() {
		@Override
	    public void handleMessage(Message msg) {
	        super.handleMessage(msg);
	        ev.setImageBitmap(bitmap);
	    }
	};
	
	
	private final class ButtonClickListener implements View.OnClickListener{

		public void onClick(View v) {
//			String path = et.getText().toString();
			try{
				new Thread(){
					@Override
					public void run(){
						//你要执行的方法
						//执行完毕后给handler发送一个空消息
						try{
							String path = et.getText().toString();
							byte[] data = ImageService.getImage(path);
							bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
							handler.sendEmptyMessage(0);
						}catch (Exception e) {
							e.printStackTrace();
							Toast.makeText(getApplicationContext(), R.string.error, Toast.LENGTH_LONG).show();
						}
					}
				}.start();
//				byte[] data = ImageService.getImage(path);
//				Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
//				ev.setImageBitmap(bitmap);//显示图片
				
			}catch (Exception e) {
				e.printStackTrace();
				Toast.makeText(getApplicationContext(), R.string.error, Toast.LENGTH_LONG).show();
			}
		}
    }
	

}

工具类

package cn.wonders.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class StreamTool {

	public static byte[] read(InputStream input) throws Exception {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while((len=input.read(buffer))!=-1) {
			baos.write(buffer, 0, len);
		}
		input.close();
		return baos.toByteArray();
	}

}



你可能感兴趣的:(android)