我需要将默认相机应用程序的意图推送到拍照,保存并返回URI。有没有办法做到这一点?安卓相机意图
安卓相机意图
回答
private static final int TAKE_PICTURE = 1;
private Uri imageUri;
public void takePhoto(View view) {
Intent intent = new Intent(MediaStore.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());
}
}
}
}
是否存在从意图打开设备的前置摄像头的能力? – Dimon 2012-09-06 11:33:54
这很好,但你可能需要: private static int TAKE_PICTURE = 1; – 2012-11-16 02:24:59
@Aleksander O. - 伟大的代码,谢谢!我发布了一个完整的功能实现这个在http://stackoverflow.com/a/13446838/1166727 – 2012-11-19 02:50:50
请尝试以下我发现here
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 0) {
String result = data.toURI();
// ...
}
}
谢谢,但拍摄的照片没有保存到一个设备,所以我得到FileNotFoundException在 Uri uri = Uri.parse(data.toURI()); bitmap = android.provider.MediaStore.Images.Media .getBitmap(contentResolver,uri); – 2010-04-28 13:27:30
您是否说您拍摄的照片没有按照您的选择存储在手机/设备上?或者发生了某些事情,即使您告诉它将图像存储在手机/设备上,它的行为就像是不存储? – Ryan 2010-04-28 14:25:04
我说图片没有被自动存储在设备上,而是作为onActivityResult()中的位图返回。不过,我找到了一个解决方案,我会在答案中提到。 – 2010-04-29 13:40:06
试试这个代码
Intent photo= new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(photo, CAMERA_PIC_REQUEST);
这篇文章被自动标记为低质量,因为它只是代码。你介意加入一些文字来解释它是如何解决问题的吗? – gung 2014-06-12 16:18:05
我花了一些时间来得到这个工作。代码几乎是从developer.android.com复制粘贴,只有一个小小的区别。
请求此权限的AndroidManifest.xml
:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在您Activity
,定义这个开始:
static final int REQUEST_IMAGE_CAPTURE = 1;
private Bitmap mImageBitmap;
private String mCurrentPhotoPath;
private ImageView mImageView;
那么火这Intent
的onClick
:
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);
}
}
添加以下支持我的ThOD:
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
。
同样适用于我。我想,你救了我几个小时。任何想法为什么Android开发人员代码提供IO异常? – Coo 2016-08-09 09:35:14
尝试,我发现这里的a link
如果您的应用程序的目标米及以上,并声明为使用未授予摄像头权限以下内容,然后尝试使用这个动作会导致SecurityException。
EasyImage.openCamera(Activity activity, int type);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
EasyImage.handleActivityResult(requestCode, resultCode, data, this, new DefaultCallback() {
@Override
public void onImagePickerError(Exception e, EasyImage.ImageSource source, int type) {
//Some error handling
}
@Override
public void onImagesPicked(List<File> imagesFiles, EasyImage.ImageSource source, int type) {
//Handle the images
onPhotosReturned(imagesFiles);
}
});
}
- 1. 安卓相机意图
- 2. 安卓相机zoom
- 3. 安卓:当意图
- 4. 安卓相机意图问题与一些设备
- 5. 安卓相机设置由意图启动时丢失
- 6. 测光相机 - 安卓
- 7. 安卓相机预览
- 8. 安卓相机的空气
- 9. 安卓相机控件
- 10. 安卓相机检测
- 11. 安卓相机翻转
- 12. 安卓相机信息
- 13. 相机预览 - 安卓
- 14. 安卓相机AutoFocus on Demand
- 15. 安卓相机菜单
- 16. 安卓与意图工作
- 17. 用安卓相机拍摄图片(意图)内存不足错误
- 18. 相机意图android
- 19. 安卓:从意向
- 20. 安卓相机预览片段
- 21. 安卓:注销相机按钮
- 22. 安卓相机预览与surfaceview
- 23. 安卓:连接的WebView用相机
- 24. 相机setParameters失败三星 - 安卓
- 25. 安卓相机上的物体识别
- 26. 安卓相机(照片未显示)
- 27. 安卓相机使用应用程序
- 28. 相机不适用于安卓7.0
- 29. 安卓相机人像方向
- 30. 安卓相机应用程序
请参考以下链接http://stackoverflow.com/questions/13977245/android-open-camera-from-button/40041381#40041381 – 2016-10-14 11:13:02