首页 > 解决方案 > 拍照并保存为 URI 时出现 NullPointerException

问题描述

我正在关注这篇博文,将此功能添加到我的应用程序中,我可以在其中拍照,然后将图片数据保存为 Uri 以获取更多功能。但是当我尝试打开相机意图时出现错误。

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

我怀疑有什么问题,file_paths.xml但我仔细检查了没有问题。我已经对通过like 保存图像的不同方式进行了一些研究,FileProvider但其中大部分对我也不起作用。

有谁知道我的代码有什么问题?

主要活动

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>

已编辑 更改为正确的路径 =“Android/data/com.example.cameraapplication/files/Pictures”

标签: javaandroidnullpointerexceptionandroid-camera

解决方案


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

在您的代码中,您有:

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

这意味着权限需要com.example.android.provider

在您的清单中,您有:

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

因此,${applicationId}不是com.example.android

你的android:authorities价值很好,但你需要你的getUriForFile()电话来匹配。一种典型的做法是:

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

推荐阅读