我要推的意图,默认的摄像头应用程序,使其拍照,保存并返回一个URI。有没有办法做到这一点?
解决方案 私有静态最终诠释TAKE_PICTURE = 1;
私人乌里imageUri;
公共无效takePhoto(查看视图){
意向意图=新的意图(android.media.action.IMAGE_CAPTURE);
档案照片=新的文件(Environment.getExternalStorageDirectory(),Pic.jpg);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(照片));
imageUri = Uri.fromFile(照片);
startActivityForResult(意向,TAKE_PICTURE);
}
@覆盖
公共无效onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(要求code,因此code,数据);
开关(要求code){
案例TAKE_PICTURE:
如果(结果code == Activity.RESULT_OK){
乌里selectedImage = imageUri;
。getContentResolver()有NotifyChange(selectedImage,NULL);
ImageView的ImageView的=(ImageView的)findViewById(R.id.ImageView);
ContentResolver的CR = getContentResolver();
点阵位图;
尝试 {
位= android.provider.MediaStore.Images.Media
.getBitmap(CR,selectedImage);
imageView.setImageBitmap(位);
Toast.makeText(这一点,selectedImage.toString()
Toast.LENGTH_LONG).show();
}赶上(例外五){
Toast.makeText(这一点,无法加载,Toast.LENGTH_SHORT)
。显示();
Log.e(相机,e.toString());
}
}
}
}
I need to push an intent to default camera application to make it take a photo, save it and return an URI. Is there any way to do this?
解决方案private static final int TAKE_PICTURE = 1;
private Uri imageUri;
public void takePhoto(View view) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, TAKE_PICTURE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ImageView imageView = (ImageView) findViewById(R.id.ImageView);
ContentResolver cr = getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
imageView.setImageBitmap(bitmap);
Toast.makeText(this, selectedImage.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
Log.e("Camera", e.toString());
}
}
}
}