获取图片后进行处理,对分辨率较大的进行缩放,较小的原图显示
1、调用系统相机
-
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
-
new DateFormat();
-
name = DateFormat.format("yyyyMMdd_hhmmss",
-
Calendar.getInstance(Locale.CHINA))
-
+ ".jpg";
-
Uri imageUri = Uri.fromFile(new File(PATH, name));
-
-
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
-
-
startActivityForResult(intent, CAMERA_TAKE);
2、调用系统相册
-
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
-
intent.setType("image/*");
-
startActivityForResult(intent, CAMERA_SELECT);
3、在onActivityResult方法处理得到的图片
看源码吧。。。
首先简单的布局文件
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
xmlns:tools="http://schemas.android.com/tools"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:orientation="vertical" >
-
-
<Button
-
android:id="@+id/btn_take"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:text="@string/take" />
-
-
<Button
-
android:id="@+id/btn_select"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:text="@string/selectFrom" />
-
-
<ScrollView
-
android:id="@+id/sv_show"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent" >
-
-
<LinearLayout
-
android:id="@+id/ll_show"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:orientation="vertical" >
-
</LinearLayout>
-
</ScrollView>
-
-
</LinearLayout>
源码
-
public class MainActivity extends Activity {
-
-
private Button btn_take, btn_select;
-
private LinearLayout ll_show;
-
-
-
private static final int CAMERA_TAKE = 1;
-
private static final int CAMERA_SELECT = 2;
-
-
-
public String name;
-
-
-
private static final String PATH = Environment
-
.getExternalStorageDirectory() + "/DCIM";
-
-
private boolean isBig = false;
-
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
-
btn_take = (Button) findViewById(R.id.btn_take);
-
btn_select = (Button) findViewById(R.id.btn_select);
-
ll_show = (LinearLayout) findViewById(R.id.ll_show);
-
-
btn_take.setOnClickListener(new OnClickListener() {
-
-
@Override
-
public void onClick(View v) {
-
takePhoto();
-
}
-
});
-
-
btn_select.setOnClickListener(new OnClickListener() {
-
-
@Override
-
public void onClick(View v) {
-
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
-
intent.setType("image/*");
-
startActivityForResult(intent, CAMERA_SELECT);
-
}
-
});
-
-
}
-
-
public void takePhoto() {
-
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
-
new DateFormat();
-
name = DateFormat.format("yyyyMMdd_hhmmss",
-
Calendar.getInstance(Locale.CHINA))
-
+ ".jpg";
-
Uri imageUri = Uri.fromFile(new File(PATH, name));
-
-
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
-
-
startActivityForResult(intent, CAMERA_TAKE);
-
}
-
-
@Override
-
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
-
super.onActivityResult(requestCode, resultCode, data);
-
-
if (resultCode == RESULT_OK) {
-
switch (requestCode) {
-
case CAMERA_TAKE:
-
Bitmap bitmap = BitmapFactory.decodeFile(PATH + "/" + name);
-
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
-
System.out.println(bitmap.getHeight() + "======"
-
+ bitmap.getWidth());
-
-
-
DisplayMetrics dm = new DisplayMetrics();
-
getWindowManager().getDefaultDisplay().getMetrics(dm);
-
-
-
float scale = bitmap.getWidth() / (float) dm.widthPixels;
-
-
Bitmap newBitMap = null;
-
if (scale > 1) {
-
newBitMap = zoomBitmap(bitmap, bitmap.getWidth() / scale,
-
bitmap.getHeight() / scale);
-
bitmap.recycle();
-
isBig = true;
-
}
-
-
final RelativeLayout rl_show_2 = new RelativeLayout(this);
-
rl_show_2.setLayoutParams(new LayoutParams(
-
RelativeLayout.LayoutParams.WRAP_CONTENT,
-
RelativeLayout.LayoutParams.WRAP_CONTENT));
-
-
ImageButton imgBtn_del_2 = new ImageButton(this);
-
imgBtn_del_2.setBackgroundResource(android.R.drawable.ic_delete);
-
-
-
RelativeLayout.LayoutParams rl_2 = new RelativeLayout.LayoutParams(
-
new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
-
RelativeLayout.LayoutParams.WRAP_CONTENT));
-
rl_2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
-
rl_2.addRule(RelativeLayout.ALIGN_RIGHT, 2);
-
-
-
-
ImageView img = new ImageView(this);
-
img.setId(2);
-
img.setLayoutParams(new LayoutParams(
-
LinearLayout.LayoutParams.WRAP_CONTENT,
-
LinearLayout.LayoutParams.WRAP_CONTENT));
-
img.setScaleType(ImageView.ScaleType.CENTER_CROP);
-
img.setPadding(2, 0, 0, 5);
-
if (isBig) {
-
img.setImageBitmap(newBitMap);
-
isBig = false;
-
} else
-
img.setImageBitmap(bitmap);
-
-
rl_show_2.addView(img);
-
rl_show_2.addView(imgBtn_del_2,rl_2);
-
-
-
ll_show.addView(rl_show_2);
-
-
imgBtn_del_2.setOnClickListener(new OnClickListener() {
-
-
@Override
-
public void onClick(View v) {
-
ll_show.removeView(rl_show_2);
-
}
-
});
-
-
break;
-
-
case CAMERA_SELECT:
-
ContentResolver resolver = getContentResolver();
-
-
-
Uri imgUri = data.getData();
-
-
try {
-
-
Bitmap photo = MediaStore.Images.Media.getBitmap(resolver,
-
imgUri);
-
-
-
DisplayMetrics dm_2 = new DisplayMetrics();
-
getWindowManager().getDefaultDisplay().getMetrics(dm_2);
-
-
-
float scale_2 = photo.getWidth() / (float) dm_2.widthPixels;
-
-
Bitmap newBitMap_2 = null;
-
if (scale_2 > 1) {
-
newBitMap_2 = zoomBitmap(photo, photo.getWidth()
-
/ scale_2, photo.getHeight() / scale_2);
-
photo.recycle();
-
isBig = true;
-
}
-
-
final RelativeLayout rl_show = new RelativeLayout(this);
-
rl_show.setLayoutParams(new LayoutParams(
-
RelativeLayout.LayoutParams.WRAP_CONTENT,
-
RelativeLayout.LayoutParams.WRAP_CONTENT));
-
-
ImageButton imgBtn_del = new ImageButton(this);
-
imgBtn_del.setBackgroundResource(android.R.drawable.ic_delete);
-
-
-
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(
-
new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
-
RelativeLayout.LayoutParams.WRAP_CONTENT));
-
rl.addRule(RelativeLayout.ALIGN_PARENT_TOP);
-
rl.addRule(RelativeLayout.ALIGN_RIGHT, 1);
-
-
-
ImageView img_2 = new ImageView(this);
-
img_2.setId(1);
-
img_2.setLayoutParams(new LayoutParams(
-
LinearLayout.LayoutParams.WRAP_CONTENT,
-
LinearLayout.LayoutParams.WRAP_CONTENT));
-
img_2.setScaleType(ImageView.ScaleType.CENTER_CROP);
-
img_2.setPadding(2, 0, 0, 5);
-
if (scale_2 > 1) {
-
img_2.setImageBitmap(newBitMap_2);
-
isBig = false;
-
} else
-
img_2.setImageBitmap(photo);
-
-
-
rl_show.addView(img_2);
-
rl_show.addView(imgBtn_del,rl);
-
-
ll_show.addView(rl_show);
-
-
imgBtn_del.setOnClickListener(new OnClickListener() {
-
-
@Override
-
public void onClick(View v) {
-
ll_show.removeView(rl_show);
-
}
-
});
-
-
} catch (Exception e) {
-
-
}
-
-
break;
-
}
-
}
-
}
-
-
-
public Bitmap zoomBitmap(Bitmap bitmap, float width, float height) {
-
-
int w = bitmap.getWidth();
-
-
int h = bitmap.getHeight();
-
-
Matrix matrix = new Matrix();
-
-
float scaleWidth = ((float) width / w);
-
-
float scaleHeight = ((float) height / h);
-
-
matrix.postScale(scaleWidth, scaleHeight);
-
-
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
-
-
return newbmp;
-
-
}
-
}
ok,结束。。
Android开发之系统相机相册使用
原文:http://blog.csdn.net/minimicall/article/details/40349047