android意图相机代码,Android相机意图

我花了几个小时才完成这项工作。 代码几乎是来自developer.android.com的复制粘贴,略有不同。

在MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath))上申请此权限:

在您的MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath))上,首先定义:

static final int REQUEST_IMAGE_CAPTURE = 1;

private Bitmap mImageBitmap;

private String mCurrentPhotoPath;

private ImageView mImageView;

然后在FileNotFoundException中解雇这个MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath)):

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

if (cameraIntent.resolveActivity(getPackageManager()) != null) {

// Create the File where the photo should go

File photoFile = null;

try {

photoFile = createImageFile();

} catch (IOException ex) {

// Error occurred while creating the File

Log.i(TAG, "IOException");

}

// Continue only if the File was successfully created

if (photoFile != null) {

cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));

startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);

}

}

添加以下支持方法:

private File createImageFile() throws IOException {

// Create an image file name

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

String imageFileName = "JPEG_" + timeStamp + "_";

File storageDir = Environment.getExternalStoragePublicDirectory(

Environment.DIRECTORY_PICTURES);

File image = File.createTempFile(

imageFileName, // prefix

".jpg", // suffix

storageDir // directory

);

// Save a file: path for use with ACTION_VIEW intents

mCurrentPhotoPath = "file:" + image.getAbsolutePath();

return image;

}

然后收到结果:

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

try {

mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));

mImageView.setImageBitmap(mImageBitmap);

} catch (IOException e) {

e.printStackTrace();

}

}

}

它的工作原理是MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath)),它与developer.android.com的代码不同。 原始代码给了我一个FileNotFoundException。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值