-1

I'm following this blog post to add this feature onto my app where I can take a picture then save the picture data as a Uri for further functions. But I'm getting an error when I try to open the camera intent.

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference

I suspect there is something wrong with the file_paths.xml but I double checked there is nothing wrong. I've done some research on different ways of saving images through like FileProvider but most of it doesn't work for me either.

Does anyone know what's wrong with my code?

MainActivity

private void openCameraIntent() {
    Intent pictureIntent = new Intent(
            MediaStore.ACTION_IMAGE_CAPTURE);
    if(pictureIntent.resolveActivity(getPackageManager()) != null){
        //Create a file to store the image
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
        }

        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,                                                         com.example.camerapplication.provider", photoFile);
            pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    photoURI);
            startActivityForResult(pictureIntent,
                    REQUEST_CAPTURE_IMAGE);
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if(requestCode == REQUEST_CAPTURE_IMAGE && resultCode == RESULT_OK){
        Glide.with(this).load(imageFilePath).into(image);
        // TODO:Use Image Uri for further functions
    }
}

private File createImageFile() throws IOException{
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    String imageFileName = "IMG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(imageFileName,".jpg",storageDir);
    imageFilePath = image.getAbsolutePath();

    return image;
}

Manifest

<uses-feature android:name="android.hardware.camera" android:required="true" />

// ... Other elements and then
// Within application scope

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths">
    </meta-data>
</provider>

file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="Android/data/com.example.cameraapplication/files/Pictures" />
</paths>

EDITED changed to proper path="Android/data/com.example.cameraapplication/files/Pictures"

3 Answers3

0

Are you sure your package name is com.example.cameraapplicationfiles?

I think it should be Android/data/com.example.cameraapplication/files/Pictures

if not try something like this.

for the authorities use full string name android:authorities="com.myCameraDemo.app".

ans for the file_paths.xml <external-path name="site_images" path="Android/data/com.myCameraDemo.app/files/Pictures" />

if it works then you know where the problem is.

Im Batman
  • 1,842
  • 1
  • 31
  • 48
0

I think the external path should be like the following. Considering your application package name is com.example.cameraapplication.

<external-path
    name="documents"
    path="Android/data/com.example.cameraapplication/files/Pictures" />
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
0

Your error means that your application ID is not com.example.android.

In your code, you have:

Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.provider", photoFile);

This means that the authority needs to be com.example.android.provider.

In your manifest, you have:

android:authorities="${applicationId}.provider"

Therefore, ${applicationId} is not com.example.android.

Your android:authorities value is fine, but you need your getUriForFile() call to match. A typical way of doing that is:

Uri photoURI = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID+".provider", photoFile);
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491