【问题标题】:Couldn't find meta-data for provider error [duplicate]找不到提供程序错误的元数据 [重复]
【发布时间】:2019-12-16 09:56:02
【问题描述】:

我正在尝试开发一个应用程序来拍摄照片,然后将它们上传到数据库。现在我被困在拍一张高质量的照片上。正如我之前读到的,要获得更好质量的照片,您需要将其保存在目录中。我正在关注Google Take Photos 根据开发人员的说明,添加所有方法和提供程序等后,我收到错误:

Couldn't find meta-data for provider with authority com.example.navigationdrawerfinal. 

我已经留下了我的应用程序的名称,所以每个人都可以看到我也在清单中更改了它。

清单:

<provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.example.navigationdrawerfinal.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

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.navigationdrawerfinal/files/Pictures" />
</paths>

以及我使用它的代码:

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.navigationdrawerfinal",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
            poza_neincarcata.setVisibility(View.GONE);
            poza_tick.setVisibility(View.VISIBLE);
        }
    }
}


private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = image.getAbsolutePath();
        return image;
    }

【问题讨论】:

  • 您在清单中将权限指定为com.example.navigationdrawerfinal.fileprovider,但FileProvider.getUriForFile() 调用使用的是com.example.navigationdrawerfinal。那些不匹配。
  • 是的@MikeM。那是我没有看到的错误。现在可以使用了,谢谢!

标签: android


【解决方案1】:

如错误所示(重点是我的):

找不到具有权限 com.example.navigationdrawerfinal 的提供商的元数据。

这是因为您为您定义的 photoURI 变量指定了错误的 FileProvider - 它应该与您在清单文件中定义的 FileProvider 完全相同,区分大小写:

Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.navigationdrawerfinal.fileprovider", // Over here
                    photoFile);

顺便说一句,最好遵循 Java 指南,以 PascalCase 格式命名您的类。

例如:

  • FileProviderfileprovider
  • MainActivitymainActivity

等等

【讨论】:

  • PascalCase 加一。我不知道为什么人们不使用这种简单而出色的命名约定......
【解决方案2】:

在我们的清单更改中像这样

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

【讨论】:

    猜你喜欢
    • 2021-12-15
    • 1970-01-01
    • 2015-06-16
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 2018-11-25
    相关资源
    最近更新 更多