`

android 调用camera相关操作

 
阅读更多
1,通过intent调用camera
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
		File photo = new File(Environment.getExternalStorageDirectory(),
				getPhotoFileName());
		Log.i(TAG,"getPhotoFileName():" +getPhotoFileName());
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
		imageUri = Uri.fromFile(photo);
		startActivityForResult(intent, GET_PHOTO_WITH_CAMERA);

private String getPhotoFileName() {
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"'IMG'_yyyy-MM-dd_HH-mm-ss");
		return dateFormat.format(date) + ".jpg";
	}


然后onActivityResult()中
if (requestCode == GET_PHOTO_WITH_CAMERA) {
				// doCropPhoto(mCurrentPhotoFile);
				Uri selectedImage = imageUri;
				Log.i(TAG, "uri:" + imageUri.toString());
				getContentResolver().notifyChange(selectedImage, null);
				ContentResolver cr = getContentResolver();
				Bitmap bitmap;
				try {
					bitmap = android.provider.MediaStore.Images.Media
							.getBitmap(cr, selectedImage);
					BitmapDrawable bd = new BitmapDrawable(bitmap);
					headerImage.setBackgroundDrawable(bd);
				} catch (Exception e) {
					Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
							.show();
				}
			}

2,如果要调用gallery去剪辑的话
doCropPhoto(photo)
	protected void doCropPhoto(File f) {
		try {
			// 启动gallery去剪辑这个照片
			final Intent intent = getCropImageIntent(Uri.fromFile(f));
			startActivityForResult(intent, GET_PHOTO_WITH_GALLARY);
		} catch (Exception e) {
		}
	}
	public static Intent getCropImageIntent(Uri photoUri) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(photoUri, "image/*");
		intent.putExtra("crop", "true");
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		intent.putExtra("outputX", 80);
		intent.putExtra("outputY", 80);
		intent.putExtra("return-data", true);
		return intent;
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics