static final int REQUEST_IMAGE_CAPTURE = 1;
private Bitmap mImageBitmap;
private String mCurrentPhotoPath;
private ImageView mImageView;
然后在 onClick 中解雇 Intent :
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 (resultCode == RESULT_OK) {
if (requestCode == 1) {
String filePath = uriFilePath.getPath(); // Here is path of your captured image, so you can create bitmap from it, etc.
}
}
}
12 回答
这是完整的代码:
请记住也要为相机添加权限 .
这是一个示例活动,它将启动相机应用程序,然后检索图像并显示它 .
请注意,相机应用程序本身使您能够查看/重新拍摄图像,一旦图像被接受,活动就会显示它 .
以下是上述活动使用的布局 . 它只是一个LinearLayout,包含一个带有按钮1的按钮和一个带有id imageview1的ImageView:
最后一个细节,一定要补充:
如果相机是您的应用功能的可选项 . 确保在权限中将require设置为false . 像这样
到您的manifest.xml .
我需要几个小时才能完成这项工作 . 它几乎是developer.android.com的复制粘贴代码,略有不同 .
在
AndroidManifest.xml
上申请此权限:在
Activity
上,首先定义:然后在
onClick
中解雇Intent
:添加以下支持方法:
然后收到结果:
使它工作的是
MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath))
,它与developer.android.com的代码不同 . 原始代码给了我一个FileNotFoundException
.拍摄照片从图库中选择:
我知道这是一个相当古老的线程,但是当用户旋转相机时,所有这些解决方案都没有完成并且在某些设备上不起作用,因为onActivityResult中的数据为空 . 所以这是我在很多设备上测试过的解决方案,到目前为止还没有遇到任何问题 .
首先在您的活动中声明您的Uri变量:
然后创建临时文件夹以存储捕获的图像,并通过摄像头捕获图像的意图:
现在这里有一个最重要的事情,你必须将你的uriFilePath保存在onSaveInstanceState中,因为如果你不这样做并且用户在使用相机时旋转了他的设备,那么你的uri将为空 .
之后,您应该始终在onCreate方法中恢复您的uri:
最后一部分是让你的Uri进入onActivityResult:
附:不要忘记为Camera和Ext添加权限 . 存储写入您的清单 .
你需要阅读Camera . (我想要做你想做的事,你必须将当前图像保存到你的应用程序,在那里进行选择/删除,然后再调用相机再试一次,而不是直接在相机内部重试 . )
这是我用于捕获和保存摄像机图像然后将其显示到imageview的代码 . 您可以根据自己的需要使用 .
您必须将摄像机图像保存到特定位置,然后从该位置获取然后将其转换为字节数组 .
这是用于打开捕获相机图像活动的方法 .
那么你的onActivityResult()方法应该是这样的 .
这是onActivityResult()中使用的getBitmap()方法 . 在获取相机捕获图像位图时,我已经完成了所有可能的性能改进 .
希望能帮助到你!
从相机中捕获照片从图库中选取图像并将其设置为布局或图像视图的背景 . 这是示例代码 .
在活动中:
在layout.xml中:
在manifest.xml中:
您可以使用带缩略图的自定义相机 . 你可以看看我的project .
使用以下代码使用移动相机捕获图片 . 如果您使用的版本高于Lolipop,您也应该添加权限请求 .
Here you can open camera or gallery and set the selected image into imageview
Add permissions in manifest
In onCreate()
Show options dialog box (to select image from camera or gallery)
Get photo from Gallery
Get photo from Camera
Once the image is get selected or captured then ,
Now its time to store the picture
Request permission