gpt4 book ai didi

android - 无法弄清楚为什么总是有值 null

转载 作者:行者123 更新时间:2023-11-29 18:45:31 29 4
gpt4 key购买 nike

尝试制作一个按钮来捕获图片并将其显示在 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;
}

最佳答案

这里的REQUEST_TAKE_PHOTO是一个requestCodeRESULT_OKresultCode。但在 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);
}

关于android - 无法弄清楚为什么总是有值 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52109832/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com