android網路圖片檢視器

久夢歌行發表於2014-12-22

可以先啟動一個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();
	}

}



相關文章