Android Can';我不明白为什么总是有空值
尝试制作按钮会捕获图片并将其显示在imageview中。我暂时保存该图像以获得更好的质量。摄像头不工作,但imageview上没有显示任何内容Android Can';我不明白为什么总是有空值,android,Android,尝试制作按钮会捕获图片并将其显示在imageview中。我暂时保存该图像以获得更好的质量。摄像头不工作,但imageview上没有显示任何内容 private void dispatchTakePictureIntent() { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (takePictureIntent.resolveActivity(getPackageMa
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;
}
这里的
REQUEST\u-TAKE\u-PHOTO
是requestCode
和RESULT\u-OK
是resultCode
。但是在activityresult的内部,您正在使用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);
}
@ʍѳђઽ૯ท 请参阅resultCode
@PankajKumar这是一个公共社区的朋友。我的意思是这里代码之间的区别是,它至少需要一些解释。无论如何,+1作为补充说明。@PankajKumar OP应该至少知道这些代码之间的区别。我很冷静,嘿!:)只是想让你补充更多细节,仅此而已。是的,我知道了。我知道它必须是resultCode,但由于某种原因我看不到它。万分感谢哪一个值是空的?