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

Get Image From Camera result null in data onActivityResult

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

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

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

所以,我必须继续搜索,直到找到

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 数据始终显示空结果。我该如何解决这个问题。可以使用第一种解决方案,但 return 大图?

谢谢

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

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

更多信息,你可以查看这个linkCapture Image from Camera and Display in Activity

试试这个,

public static int REQUEST_CAMERA = 111;

private void cameraIntent() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, REQUEST_CAMERA);
}

onStartActivityForResult 代码

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {
            _FileName = "image file";
            capturePic = null;
            file_pdf = null;

            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            //mImageView.setImageBitmap(imageBitmap);
            img_ticket_detail.setImageBitmap(imageBitmap);
            capturePic = imageBitmap;
        }

    }

}

你也可以使用滑行库来展示图片