package com.kwstu.palmjn;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class UpladeActivity extends Activity {
private Button update_btn;
private ImageView img_img;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.uploadactivity);
findAll();
bind();
}
public void findAll() {
update_btn = (Button) this .findViewById(R.id.update_btn);
img_img = (ImageView) this .findViewById(R.id.img_img);
}
public void bind() {
update_btn.setOnClickListener(mylistener);
}
private View.OnClickListener mylistener = new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.update_btn:
Thread th1 = new Thread( new mythread());
th1.start();
break ;
default :
break ;
}
}
};
Handler hd = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
// super.handleMessage(msg);
if (msg.what == 123 ) {
String jason = msg.obj.toString();
String filepath = Environment.getExternalStorageDirectory()
+ File.separator + jason;
Bitmap bitmap1 = BitmapFactory.decodeFile(filepath);
img_img.setImageBitmap(bitmap1);
}
}
};
class mythread implements Runnable {
public void run() {
// TODO Auto-generated method stub
HttpClient hc = new DefaultHttpClient();
HttpPost hp = new HttpPost(wcf地址);
HttpResponse hr;
InputStreamEntity reqEntity;
String path = 上传文件路径;
File f = new File(path);
if (f.exists()) {
System.out.println( "successful" );
try {
reqEntity = new InputStreamEntity( new FileInputStream(path), - 1 );
reqEntity.setContentType( "binary/octet-stream" );
reqEntity.setChunked( true ); // Send in multiple parts if needed
hp.setEntity(reqEntity);
System.out.println( "4" );
HttpResponse response = hc.execute(hp);
System.out.println( "5" );
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
//e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
}
}
|