从数据onActivityResult中获取相机结果的null结果

时间:2017-11-08 04:34:06

标签: android android-camera onactivityresult

我想使用

从相机获取图像
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);
}

On OnctivityResult数据始终显示null结果。我如何解决这个问题。可以使用第一个解决方案,但返回更大的图像?

由于

2 个答案:

答案 0 :(得分:3)

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

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

有关详细信息,请查看此链接Here

答案 1 :(得分:2)

试试这个,

latin-1

onStartActivityForResult的代码

public static int REQUEST_CAMERA = 111;

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

你也可以使用滑动库来显示图像