Skip to content Skip to sidebar Skip to footer

Android Capturing An Image From Camera Returns Empty Data

While Capturing image in android app intent return null each time. Please see below the code which I am using. I have tried multiple ways to get the permission as well as intent se

Solution 1:

Try to use the following code for getting the image in all APIs -

publicvoidtakePicture() {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            file = FileProvider.getUriForFile(mContext,
                    BuildConfig.APPLICATION_ID + ".provider",
                    getOutputMediaFile());
            intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
            startActivityForResult(intent, TAKE_IMAGE_REQUEST);
        }

 private File getOutputMediaFile() {
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), getString(R.string.app_folder_name));

        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                returnnull;
            }
        }


        returnnew File(mediaStorageDir.getPath() + File.separator +
                PROFILE_PIC + getString(R.string.pic_type));
    }

And on your onActivityResult -

@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case TAKE_IMAGE_REQUEST:
                if (resultCode == RESULT_OK) {
                    imageView.setImageURI(file);
                }
                break;

            default:
                break;

        }
    }

Also, you need to define the Provider in your Manifest.xml file -

<providerandroid:name="android.support.v4.content.FileProvider"android:authorities="${applicationId}.provider"android:exported="false"android:grantUriPermissions="true"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/provider_paths"/></provider>

And finally the provider_paths.xml will be as follows -

<?xml version="1.0" encoding="utf-8"?><pathsxmlns:android="http://schemas.android.com/apk/res/android"><external-pathname="external_files"path="."/></paths>

Let me know in case you need any clarification.

Post a Comment for "Android Capturing An Image From Camera Returns Empty Data"