//相册
tv_editIcon_xiangce.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent("android.intent.action.PICK");
intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*");
intent.putExtra("output", Uri.fromFile(sdcardTempFile));
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 3);// 裁剪框比例
intent.putExtra("aspectY", 4);
intent.putExtra("outputX", 120);// 输出图片大小
intent.putExtra("outputY", 160);
startActivityForResult(intent, 100);
}
});
}
//拍照
tv_editIcon_paizhao.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 指定调用相机拍照后照片的储存路径
cameraintent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(tempFile));
startActivityForResult(cameraintent, 101);
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100) {
// Drawable draw = Drawable.createFromPath(sdcardTempFile.getAbsolutePath());
Bitmap bmp = BitmapFactory.decodeFile(sdcardTempFile.getAbsolutePath());
// imageView.setImageBitmap(bmp);
iv_editIcon_icon.setBackgroundDrawable(new BitmapDrawable(bmp));
currentIcon = sdcardTempFile.getAbsolutePath();
}else if(requestCode == 101){
// 选择拍照
startPhotoZoom(Uri.fromFile(tempFile));
}else if(requestCode == 102){
if (data != null)
// setPicToView(data);
sentPicToNext(data);
}
}
private void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// crop为true是设置在开启的intent中设置显示的view可以剪裁
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 3);
intent.putExtra("aspectY", 4);
// outputX,outputY 是剪裁图片的宽高
intent.putExtra("outputX", 60);
intent.putExtra("outputY", 80);
intent.putExtra("return-data", true);
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, 102);
}
// 将进行剪裁后的图片传递到下一个界面上
private void sentPicToNext(Intent picdata) {
Bundle bundle = picdata.getExtras();
if (bundle != null) {
Bitmap photo = bundle.getParcelable("data");
if (photo==null) {
}else {
iv_editIcon_icon.setBackgroundDrawable(new BitmapDrawable(photo));
File myCaptureFile = new File(Environment.getExternalStorageDirectory(),fileName);
try {
saveMyBitmap(photo,myCaptureFile);
currentIcon = myCaptureFile.getAbsolutePath();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//保存图片到本地
public void saveMyBitmap(Bitmap bm,File file) throws IOException {
file.createNewFile();
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bm.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}