无法获取提供程序 androidx.core.content.FileProvider: java.lang.Illeg



我看过很多喜欢这个的帖子,但仍然不知道问题是什么。我尝试在file_paths.xml更改路径和名称。

file_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="pirosfogo_images"
path="storage/emulated/0/pictures/"/>
</paths>

AndroidManifest.xml:

<application
...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.kijevigombooc.pirosfogo.fileprovider"
android:exported="false"
android:grantUriPermissions="true"/>
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</application>

.java:

void takePhoto(){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 110);
}
else
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null)
{
File photoFile = createPhotoFile();
if(photoFile != null){
pathToFile = photoFile.getAbsolutePath();
Uri photoURI = FileProvider.getUriForFile(ProfileEdit.this, "adada", photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(intent, REQUEST_CAMERA);
}
}
}
}
private File createPhotoFile() {
String name = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File storageDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = null;
try{
image = File.createTempFile(name, ".jpg", storageDir);
} catch(Exception e){}
return image;
}

为什么我会收到错误?

这是因为您在错误的父标签(<application>标签(中指定了<meta-data>标签。应在<provider>标记中指定它。(下面的两个代码片段显示了差异:(

<application
...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.kijevigombooc.pirosfogo.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>

<application
...
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.kijevigombooc.pirosfogo.fileprovider"
android:exported="false"
android:grantUriPermissions="true"/>
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</application>

最新更新