Java 拍摄照片并另存为URI时的NullPointerException

Java 拍摄照片并另存为URI时的NullPointerException,java,android,nullpointerexception,android-camera,Java,Android,Nullpointerexception,Android Camera,我在下面的博客文章中将此功能添加到我的应用程序中,我可以在其中拍照,然后将图片数据保存为Uri,以备将来使用。但是当我试图打开相机时,我遇到了一个错误 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager,java.la

我在下面的博客文章中将此功能添加到我的应用程序中,我可以在其中拍照,然后将图片数据保存为Uri,以备将来使用。但是当我试图打开相机时,我遇到了一个错误

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager,java.lang.String)”

我怀疑
文件\u path.xml有问题,但我仔细检查了一下,没有问题。我曾经研究过通过
FileProvider
等不同的方式保存图像,但大多数都不适用于我

有人知道我的代码出了什么问题吗

main活动

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;
}
清单

<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>

// ... 其他元素,然后
//适用范围内
文件路径.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>

已编辑
更改为property path=“Android/data/com.example.cameraapplication/files/Pictures”

您确定您的包名是
com.example.cameraapplicationfiles

我想应该是安卓/data/com.example.cameraapplication/files/Pictures

如果你不喜欢,试试这样的

对于权限,请使用完整的字符串名称 android:authorities=“com.myCameraDemo.app”。

文件_paths.xml的ans


如果有效,那么您就知道问题所在。

我认为外部路径应该如下所示。考虑到您的应用程序包名是
com.example.cameraapplication

<external-path
    name="documents"
    path="Android/data/com.example.cameraapplication/files/Pictures" />

您的错误意味着您的应用程序ID不是
com.example.android

在代码中,您有:

Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.provider", photoFile);
android:authorities="${applicationId}.provider"
这意味着权限必须是
com.example.android.provider

在您的舱单中,您有:

Uri photoURI = FileProvider.getUriForFile(this, "com.example.android.provider", photoFile);
android:authorities="${applicationId}.provider"
因此,
${applicationId}
不是
com.example.android

您的
android:authorities
值没有问题,但需要调用
getUriForFile()
来匹配。一种典型的方法是:

Uri photoURI = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID+".provider", photoFile);

是的,我忘了将“/”添加到我的路径目录中,并且我尝试了根据你的建议进行更改。不幸的是,它仍然会给我相同的错误。请不要使用此选项。请在清单文件
com.myCameraDemo.app
中尝试您的包名,您的意思是
com.cameraapplication.app
?因此在我的权限中将是
android:authorities=”com.cameraapplication.app.provider“
?您的应用程序包名称是什么?
com.example.cameraapplication