private byte [] mContent = new byte [1024 ];
private Bitmap myBitmap;
private static final int REQUEST_CAMERA = 1 ;
private static final int REQUEST_CALENDAR = 2 ;
public void camera () {
final CharSequence[] items = { "相册" , "拍照" };
AlertDialog dlg = new AlertDialog.Builder(PersonDataActivity.this )
.setTitle("选择图片" )
.setItems(items, new DialogInterface.OnClickListener() {
public void onClick (DialogInterface dialog, int item) {
if (item == 1 ) {
Intent getImageByCamera = new Intent(
"android.media.action.IMAGE_CAPTURE" );
startActivityForResult(getImageByCamera,
REQUEST_CAMERA);
} else {
Intent getImage = new Intent(
Intent.ACTION_GET_CONTENT);
getImage.addCategory(Intent.CATEGORY_OPENABLE);
getImage.setType("image/jpeg" );
startActivityForResult(getImage, 0 );
}
}
}).create();
dlg.show();
}
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
super .onActivityResult(requestCode, resultCode, data);
ContentResolver resolver = getContentResolver();
if (requestCode == 0 ) {
try {
Uri originalUri = data.getData();
startPhotoZoom(originalUri);
Bitmap bm = yasuo(originalUri);
Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(
getContentResolver(), bm, null , null ));
mContent = CameraUtils.readStream(resolver.openInputStream(Uri
.parse(uri.toString())));
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == REQUEST_CAMERA) {
try {
super .onActivityResult(requestCode, resultCode, data);
Bundle extras = data.getExtras();
myBitmap = (Bitmap) extras.get("data" );
ByteArrayOutputStream baos = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 100 , baos);
mContent = baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == REQUEST_CALENDAR) {
if (requestCode == RESULT_OK) {
}
}
if (data != null ) {
getImageToView(data);
}
HashMap<String, Object> map = service.updateAvatar(uid, mContent,
"android" , String.valueOf(1020 ));
if (map != null && !"" .equals(map.get("newAvatar" ).toString())) {
spf = getSharedPreferences("data" , 0 );
Editor e = spf.edit();
e.putString("avatar" , map.get("newAvatar" ).toString());
e.commit();
} else {
Utils.showToast(PersonDataActivity.this , "上传头像失败" );
}
}
/**
* 裁剪图片方法实现
*
* @param uri
*/
public void startPhotoZoom (Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP" );
intent.setDataAndType(uri, "image/*" );
intent.putExtra("crop" , "true" );
intent.putExtra("aspectX" , 1 );
intent.putExtra("aspectY" , 1 );
intent.putExtra("outputX" , 320 );
intent.putExtra("outputY" , 320 );
intent.putExtra("return-data" , true );
startActivityForResult(intent, 2 );
}
/**
* 保存裁剪之后的图片数据
*
* @param picdata
*/
private void getImageToView (Intent data) {
Bundle extras = data.getExtras();
if (extras != null ) {
Bitmap photo = extras.getParcelable("data" );
Drawable drawable = new BitmapDrawable(photo);
img_touxiang.setImageDrawable(drawable);
}
}
/**
*压缩图片
*
private Bitmap yasuo(Uri uri) {
Bitmap bitmap = null;
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeStream(this.getContentResolver()
.openInputStream(uri), null, options);
int picWidth = options.outWidth;
int picHeight = options.outHeight;
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
options.inSampleSize = 1;
if (picWidth > picHeight) {
if (picWidth > screenWidth)
options.inSampleSize = picWidth / screenWidth;
} else {
if (picHeight > screenHeight)
options.inSampleSize = picHeight / screenHeight;
}
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(this.getContentResolver()
.openInputStream(uri), null, options);
img_touxiang.setImageBitmap(bitmap);
/*
* if (bitmap.isRecycled() == false) { bitmap.recycle(); }
*/
System.gc();
} catch (Exception e1) {
}
return bitmap;
}