Android camera capture activity returns null Uri

99封情书 提交于 2019-12-04 09:07:20

You have to tell the camera, where to save the picture and remeber the uri yourself:

private Uri mMakePhotoUri;

private File createImageFile() {
    // return a File object for your image.
}

private void makePhoto() {
    try {
        File f = createImageFile();
        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        mMakePhotoUri = Uri.fromFile(f);
        i.putExtra(MediaStore.EXTRA_OUTPUT, mMakePhotoUri);
        startActivityForResult(i, REQUEST_MAKE_PHOTO);
    } catch (IOException e) {
        Log.e(TAG, "IO error", e);
        Toast.makeText(getActivity(), R.string.error_writing_image, Toast.LENGTH_LONG).show();
    }
}

@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    switch (requestCode) {
        case REQUEST_MAKE_PHOTO:
            if (resultCode == Activity.RESULT_OK) {
                // do something with mMakePhotoUri
            }
            return;
        default: // do nothing
            super.onActivityResult(requestCode, resultCode, data);
    }
}

You should save the value of mMakePhotoUri over instance states withing onCreate() and onSaveInstanceState().

Inject this extra into the Intent that called onActivityResult and the system will do all the heavy lifting for you.

File f = createImageFile();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));

Doing this makes it as easy as this to retrieve the photo as a bitmap.

private void handleSmallCameraPhoto(Intent intent) {
    Bundle extras = intent.getExtras();
    mImageBitmap = (Bitmap) extras.get("data");
    mImageView.setImageBitmap(mImageBitmap);
}

dont pass any extras, just define the path where you have placed or saved the file directly in onActivityResult

 public void openCamera() {

    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    file = createImageFile();
    boolean isDirectoryCreated = file.getParentFile().mkdirs();
    Log.d("", "openCamera: isDirectoryCreated: " + isDirectoryCreated);
    if (Build.VERSION.SDK_INT >= 23) {
        tempFileUri = FileProvider.getUriForFile(getActivity().getApplicationContext(),
                "com.scanlibrary.provider", // As defined in Manifest
                file);
    } else {
        tempFileUri = Uri.fromFile(file);
    }

    try
    {
        cameraIntent.putExtra("return-data", true);
        startActivityForResult(cameraIntent, ScanConstants.START_CAMERA_REQUEST_CODE);
    }
    catch (Exception e)
    {
    }
}

private File createImageFile() {
    clearTempImages();
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new
            Date());
    File file = new File(ScanConstants.IMAGE_PATH, "IMG_" + timeStamp +
            ".jpg");
    fileUri = Uri.fromFile(file);
    return file;
}

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

    Bitmap bitmap = null ;
    if (resultCode == Activity.RESULT_OK ) {
        try {
            if (Build.VERSION.SDK_INT >= 23) {
                    tempFileUri = FileProvider.getUriForFile(getActivity().getApplicationContext(),
                            "com.scanlibrary.provider", // As defined in Manifest
                            file);
                } else {
                    tempFileUri = Uri.fromFile(file);
                }

                    bitmap = getBitmap(tempFileUri);



                    bitmap = getBitmap(data.getData());

        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        getActivity().finish();
    }
    if (bitmap != null) {
        postImagePick(bitmap);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!