package lsw.ui.account;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.Base64;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class AccountActivity extends Activity {
public static final int NONE = 0;
public static final int PHOTOHRAPH = 1;// 拍照
public static final int PHOTOZOOM = 2; // 缩放
public static final int PHOTORESOULT = 3;// 结果
public static final String IMAGE_UNSPECIFIED = "image/*";
private SharedPreferences spUserInfo;
private int eid;
private Account account;
private ImageView ImgPhotoAccount;//头像
private LswAndroidHelper lswAndroidHelper;
private MemberHelper memberHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Init();
}
/**
* 初始化
*/
public void Init()
{
setContentView(R.layout.account);
//eid
memberHelper = new MemberHelper(this);
eid = memberHelper.getInt("eid");
//控件
ImgPhotoAccount = (ImageView)findViewById(R.id.ImgPhotoAccount);
//上传照片
ImgPhotoAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new AlertDialog.Builder(AccountActivity.this)
.setTitle("请选择")
.setIcon(android.R.drawable.ic_dialog_info)
.setSingleChoiceItems(new String[] {"相册","拍照"}, 0,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
switch(which)
{
case 0:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
IMAGE_UNSPECIFIED);
startActivityForResult(intent, PHOTOZOOM);
break;
case 1:
Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
Environment.getExternalStorageDirectory(), "temp.jpg")));
//System.out.println("=============" + Environment.getExternalStorageDirectory());
startActivityForResult(intent2, PHOTOHRAPH);
break;
}
}
}
)
.setNegativeButton("取消", null)
.show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
// 拍照
if (requestCode == PHOTOHRAPH) {
// 设置文件保存路径这里放在跟目录下
File picture = new File(Environment.getExternalStorageDirectory()
+ "/temp.jpg");
System.out.println("------------------------" + picture.getPath());
startPhotoZoom(Uri.fromFile(picture));
}
if (data == null)
return;
// 读取相册缩放图片
if (requestCode == PHOTOZOOM) {
startPhotoZoom(data.getData());
}
// 处理结果
if (requestCode == PHOTORESOULT) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 -
// 100)压缩文件
ImgPhotoAccount.setImageBitmap(photo);
UploadPhotoThread uploadPhotoThread = new UploadPhotoThread(photo);
Thread thread = new Thread(uploadPhotoThread);
thread.start();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
public void imgUpload(Bitmap photo){
try{
//String srcUrl = "/sdcard/"; //路径
String fileName = eid+".jpg"; //文件名
//FileInputStream fis = new FileInputStream(srcUrl + fileName);
InputStream fis = FormatTools.getInstance().Bitmap2InputStream(photo);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count = 0;
while((count = fis.read(buffer)) >= 0){
baos.write(buffer, 0, count);
}
String uploadBuffer = new String(Base64.encode(baos.toByteArray(),Base64.DEFAULT)); //进行Base64编码
//System.out.println("uploadBuffer-->"+uploadBuffer);
upload(fileName, uploadBuffer);
fis.close();
}catch(Exception e){
e.printStackTrace();
}
}
private Handler uploadHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
//上传成功后处理逻辑
// switch(msg.what)
// {
// case 1:
//
// break;
// }
}
};
class UploadPhotoThread implements Runnable
{
private Bitmap photo;
public UploadPhotoThread(Bitmap photo)
{
this.photo = photo;
}
@Override
public void run() {
// TODO Auto-generated method stub
imgUpload(photo);
Message message = new Message();
message.what = 1;
try
{
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
uploadHandler.sendMessage(message);
}
}
public void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 64);
intent.putExtra("outputY", 64);
intent.putExtra("return-data", true);
startActivityForResult(intent, PHOTORESOULT);
}
public String upload(String fileName, String imageBuffer){
String nameSpace = "http://tempuri.org/";
String methodName = AccountActivity.this.getResources().getString(R.string.uploadphoto_method);
String soapAction = "http://tempuri.org/UploadPhoto";
String url = AccountActivity.this.getResources().getString(R.string.uploadphoto_server);//后面加不加那个?wsdl参数影响都不大
//建树webservice连接对象
org.ksoap2.transport.HttpTransportSE transport = new HttpTransportSE(url);
transport.debug = true;//是否是调试模式
//设置连接参数
SoapObject soapObject = new SoapObject(nameSpace,methodName);
soapObject.addProperty("fileName", fileName); //参数1 图片名
soapObject.addProperty("imageBuffer", imageBuffer); //参数2 图片字符串
soapObject.addProperty("apiKey",new LswSetting().APIKEY);
//so
评论19
最新资源