您的位置:首页 > 编程语言

调用系统相机以及 打开相册的代码

2015-06-03 20:45 435 查看
private void doPhoto(int requestCode, Intent data) {

if (requestCode == SELECT_PIC_BY_PICK_PHOTO) // 从相册取图片,有些手机有异常情况,请注意
{
if (data == null) {
Toast.makeText(this, "选择图片文件出错", Toast.LENGTH_LONG).show();
return;
}
photoUri = data.getData();
if (photoUri == null) {
Toast.makeText(this, "选择图片文件出错", Toast.LENGTH_LONG).show();
return;
}
}
String[] pojo = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(photoUri, pojo, null, null, null);

if (cursor != null) {
int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
cursor.moveToFirst();
picPath = cursor.getString(columnIndex);
// cursor.close();
Log.v("picPath", "-->>" + picPath);

try {
// 4.0以上的版本会自动关闭 (4.0--14;; 4.0.3--15)
if (Integer.parseInt(Build.VERSION.SDK) < 14) {
cursor.close();
}
} catch (Exception e) {
Log.v("==", "error:" + e);
}
}
/*
* if(picPath != null && ( picPath.endsWith(".png") ||
* picPath.endsWith(".PNG") ||picPath.endsWith(".jpg")
* ||picPath.endsWith(".JPG") ))
*/
// if (picPath != null) {
// Intent intent = new Intent();
// intent.putExtra("picPath", picPath);
// setResult(1, intent);
// finish();
// } else {
// Toast.makeText(this, "选择文件不正确!", Toast.LENGTH_LONG).show();
// }
}

// 接收拍摄照片界面信息
private void decodeImg() {
// bm = BitmapFactory.decodeFile(picPath, MainActivity.getOptions());
// int width = takePhotoImg.getWidth();
// int height = takePhotoImg.getHeight();
// takePhotoImg.setVisibility(View.GONE);
// layout1.setVisibility(View.VISIBLE);
// layout1.setLayoutParams(new LinearLayout.LayoutParams(width,
// height));
// dwfc_img.setImageBitmap(bm);
if (!"".equals(picPath)) {

System.out.println("------->aaa+++"+picPath);
image_photo.setVisibility(View.VISIBLE);
image_photo.setScaleType(ScaleType.FIT_XY);

// imageView.setImageURI(photoUri);
Bitmap bt = BitmapFactory.decodeFile(picPath);
image_photo.setImageBitmap(bt);

}
}

/**
* 拍照获取图片
*/
private void takePhoto() {
// 执行拍照前,应该先判断SD卡是否存在
String SDState = Environment.getExternalStorageState();
if (SDState.equals(Environment.MEDIA_MOUNTED)) {

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// "android.media.action.IMAGE_CAPTURE"
/***
* 需要说明一下,以下操作使用照相机拍照,拍照后的图片会存放在相册中的 这里使用的这种方式有一个好处就是获取的图片是拍照后的原图
* 如果不实用ContentValues存放照片路径的话,拍照后获取的图片为缩略图不清晰
*/
ContentValues values = new ContentValues();
photoUri = this.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
System.out.println("-------->" + photoUri);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);
/** ----------------- */
startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO);
} else {
Toast.makeText(this, "内存卡不存在", Toast.LENGTH_LONG).show();
}
}

/***
* 从相册中取图片
*/
private void pickPhoto() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {

//			if(flag){
//				decodeImg();
//				flag =false;
//			}
//			else {
doPhoto(requestCode, data);
Toast.makeText(getApplicationContext(), "ddd"+picPath, 0).show();
System.out.println("sss"+picPath);
decodeImg();
//			}
}
}
/*@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("picPath", picPath);
}*/


以上 就是 选择相册图片和 拍照的代码 其中拍照的代码 在部分手机(例如三星) 上不行 待研究

调用的时候 选择相册就调用 picPhoto(); 拍照就 takephoto();

decodeImg(); 是用来显示照片的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: