gpt4 book ai didi

android - 从相机获取图像结果在数据 onActivityResult 中为空

转载 作者:太空狗 更新时间:2023-10-29 16:27:24 26 4
gpt4 key购买 nike

我想从相机获取图像,使用

public void LicenseCameraIntent() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, PROFILE_REQUEST_CAMERA);
}

返回位图,但效果很好。不幸的是,图像显示的尺寸非常小。

所以,我必须继续寻找,直到我得到

public void licenseCameraIntent() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Uri mImageCaptureUri1 = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));

cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri1);
cameraIntent.putExtra("return-data", true);
startActivityForResult(cameraIntent, LICENSE_REQUEST_CAMERA);
}

OnActivityResult 数据总是显示空结果。我该如何解决这个问题。可以使用第一个解决方案但返回更大的图像吗?

谢谢

最佳答案

当您从相机捕获图像时,您已经为图像创建了一个文件,该文件是您的图像完整路径,因此将其设为全局

 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);
}
}

和图片文件,mCurrentPhotoPath是图片的完整路径

  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;
}

在 onActivityResult() 之后,您从 mCurrentPhotoPath 获取图像

    @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();
}
}
}

有关更多信息,您可以查看此链接 Capture Image from Camera and Display in Activity

关于android - 从相机获取图像结果在数据 onActivityResult 中为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47171651/

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