public void onClick(View view) {
switch (view.getId()) {
//点击本地图片
case R.id.localbutton:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 2);
dismiss();
break;
//点击相机添加图片
case R.id.photobutton:
Intent intentphoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intentphoto, 1);
dismiss();
break;
//返回
case R.id.canclebutton:
cancel();
break;
}
}
}
//接受图片内容
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case 2: //sd卡获取的图片URI
myDialog.cancel();
imageBackground.setVisibility(View.GONE);
twoCodeImage.setVisibility(View.VISIBLE);
Uri uri = data.getData();
getImage(uri); //该方法将图片URI转为图片
break;
case 1: //相机获取的图片数据,直接填充imageview
myDialog.cancel();
imageBackground.setVisibility(View.GONE);
twoCodeImage.setVisibility(View.VISIBLE);
Bundle extras = data.getExtras();
bitmap = (Bitmap) extras.get("data");
twoCodeImage.setImageBitmap(bitmap);
break;
default:
break;
}
}
}
private void getImage(Uri paramUri) {
int j = 1;
if ((this.bitmap != null) && (!this.bitmap.isRecycled())) {
this.bitmap.recycle();
this.bitmap = null;
System.gc();
}
ContentResolver localContentResolver = getContentResolver();
BitmapFactory.Options localOptions1 = new BitmapFactory.Options();
localOptions1.inJustDecodeBounds = true;
try {
Bitmap localBitmap1 = BitmapFactory.decodeStream(
localContentResolver.openInputStream(paramUri), null,
localOptions1);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if ((localOptions1.outHeight > 480) || (localOptions1.outWidth > 480)) {
int k = localOptions1.outHeight;
int m = localOptions1.outWidth;
double d1 = Math.max(k, m);
double d2 = Math.log(480.0D / d1);
double d3 = Math.log(0.5D);
double d4 = (int) Math.round(d2 / d3);
j = (int) Math.pow(2.0D, d4);
BitmapFactory.Options localOptions2 = new BitmapFactory.Options();
localOptions2.inSampleSize = j * 2;
localOptions2.inJustDecodeBounds = false;
Bitmap localBitmap2;
try {
localBitmap2 = BitmapFactory.decodeStream(
localContentResolver.openInputStream(paramUri), null,
localOptions2);
this.bitmap = localBitmap2;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
if (paramUri.getScheme().equals("content")) {
Cursor cursor = getContentResolver().query(paramUri, null,
null, null, null);
cursor.moveToFirst();
filePath = cursor.getString(1);
cursor.close();
} else {
filePath = paramUri.toString();
filePath = filePath.replace("file://", "");
}
this.bitmap = BitmapFactory.decodeFile(filePath);
int width = 480;
float scaleWidth = 0;
float scaleHeight = 0;
int w = bitmap.getWidth();
if (w * 1.5 <= 480) {
scaleWidth = (float) width / w;
scaleHeight = (float) width / w;
} else {
scaleWidth = 1;
scaleHeight = 1;
}
if (!(scaleWidth == 1 && scaleHeight == 1)) {
Matrix matrix = new Matrix(); // 创建操作图片用的Matrix对象
matrix.postScale(scaleWidth, scaleHeight);
if (bitmap.getWidth() < bitmap.getHeight())
matrix.postRotate(90); // 设置缩放比例
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);// 建立新的bitmap,其内容是对原bitmap的缩放后的图
this.bitmap = newbmp;
}
}
if (this.bitmap != null) {
twoCodeImage.setImageBitmap(bitmap);
}
}