界面布局:一个照相按钮,一个从相册选择图片按钮,一个ImageView用来显示图片,代码和图片不再给出
1、用相机拍摄照片并显示(解释见代码注释)
mTakePhotoButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// new一个File用来存放拍摄到的照片
// 通过getExternalStorageDirectory方法获得手机系统的外部存储地址
File imageFile = new File(Environment
.getExternalStorageDirectory(), "tempImage.jpg");
// 如果存在就删了重新创建
try {
if (imageFile.exists()) {
imageFile.delete();
}
imageFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
// 将存储地址转化成uri对象
imageUri = Uri.fromFile(imageFile);
// 设置打开照相的Intent
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
// 设置相片的输出uri为刚才转化的imageUri
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
// 开启相机程序,设置requestCode为TOKE_PHOTO
startActivityForResult(intent, TAKE_PHOTO);
}
});
startActivityForResult(intent, CROP_PHOTO);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
// 从拍照界面返回
case TAKE_PHOTO:
if (resultCode == RESULT_OK) {
// 设置intent为启动裁剪程序
Intent intent = new Intent("com.android.camera.action.CROP");
// 设置Data为刚才的imageUri和Type为图片类型
intent.setDataAndType(imageUri, "image/*");
// 设置可缩放
intent.putExtra("scale", true);
// 设置输出地址为imageUri
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
// 开启裁剪,设置requestCode为CROP_PHOTO
startActivityForResult(intent, CROP_PHOTO);
}
break;
// 从裁剪界面返回
case CROP_PHOTO:
if (resultCode == RESULT_OK) {
Bitmap bitmap;
try {
//通过BitmapFactory将imageUri转化成bitmap
bitmap = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(imageUri));
//设置显示
mPhotoImageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
default:
break;
}
}
2、从相册选取照片显示
按钮点击的方法
mChoosePhotoButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//同样new一个file用于存放照片
File imageFile = new File(Environment
.getExternalStorageDirectory(), "outputImage.jpg");
if (imageFile.exists()) {
imageFile.delete();
}
try {
imageFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
}
//转换成Uri
imageUri = Uri.fromFile(imageFile);
//开启选择呢绒界面
Intent intent = new Intent("android.intent.action.GET_CONTENT");
//设置可以缩放
intent.putExtra("scale", true);
//设置可以裁剪
intent.putExtra("crop", true);
intent.setType("image/*");
//设置输出位置
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
//开始选择
startActivityForResult(intent, CHOOSE_PHOTO);
}
});
}
本来处理uri时和拍摄照片用的同样的方法,通过BitmapFactory转换成bitmap对象,发现不可行,好像是android版本的原因,生成的Path不一样,然后百度了好久找不到解决方案,最后谷歌瞬间找到很多网页讲述这个的,不得不说以后搜索还是用谷歌好。先上代码
onActivityResult
case CHOOSE_PHOTO:
if(resultCode==RESULT_OK){
handleImageOnKitkat(data);
}
break;
@TargetApi(Build.VERSION_CODES.KITKAT)
private void handleImageOnKitkat(Intent data) {
String imagePath = null;
Uri uri = data.getData();
if (DocumentsContract.isDocumentUri(this, uri)) {
// 如果是document类型的Uri,则通过document id处理
String docId = DocumentsContract.getDocumentId(uri);
if ("com.android.providers.media.documents".equals(uri
.getAuthority())) {
String id = docId.split(":")[1]; // 解析出数字格式的id
String selection = MediaStore.Images.Media._ID + "=" + id;
imagePath = getImagePath(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
} else if ("com.android.providers.downloads.documents".equals(uri
.getAuthority())) {
Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"),
Long.valueOf(docId));
imagePath = getImagePath(contentUri, null);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
// 如果不是document类型的Uri,则使用普通方式处理
imagePath = getImagePath(uri, null);
}
displayImage(imagePath); // 根据图片路径显示图片
System.err.println(imagePath);
}
private String getImagePath(Uri uri, String selection) {
String path = null;
// 通过uri和selection来获取真实的图片路径
Cursor cursor = getContentResolver().query(uri, null, selection, null,
null);
if (cursor != null) {
if (cursor.moveToFirst()) {
path = cursor.getString(cursor
.getColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close();
}
return path;
}
private void displayImage(String imagePath) {
if (imagePath != null) {
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
mPhotoImageView.setImageBitmap(bitmap);
} else {
Toast.makeText(this, "failed to get image", Toast.LENGTH_SHORT)
.show();
}
}
目前这段代码看的似懂非懂,先存下来,留着进一步学习!
程序截图
一个多项的dialog的使用
<span style="font-size:14px;">new AlertDialog.Builder(this).setItems(
new String[] { "拍摄照片", "从相册选择" },
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
takePhoto();
break;
case 1:
getPhotoFromAlbum();
break;
default:
break;
}
}
}).show();</span><span style="font-size: 18px;">
</span>