尝试使按钮拍摄照片并在imageview中显示。我正在临时保存该图像以具有更好的质量。相机无法正常工作,但imageview上没有任何显示
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.l_sliuzas.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_TAKE_PHOTO && requestCode == RESULT_OK)
{
Bitmap bitmapas = BitmapFactory.decodeFile(mCurrentPhotoPath);
mImageView.setImageBitmap(bitmapas);
}
}
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 = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
答案 0 :(得分:2)
这里REQUEST_TAKE_PHOTO
是requestCode
,而RESULT_OK
是resultCode
。但是在onActivityResult
内部,您正在使用requestCode
进行评估。哪个不正确。
所以下面的代码不正确
if (requestCode == REQUEST_TAKE_PHOTO && requestCode == RESULT_OK)
{
Bitmap bitmapas = BitmapFactory.decodeFile(mCurrentPhotoPath);
mImageView.setImageBitmap(bitmapas);
}
将其更改为
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK)
{
Bitmap bitmapas = BitmapFactory.decodeFile(mCurrentPhotoPath);
mImageView.setImageBitmap(bitmapas);
}