2

I've looked at many posts liks this, but still don't know what the problem is. I Tried changing path and name in 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;
}

Why do I get the error?

KijeviGombooc
  • 304
  • 2
  • 13

1 Answers1

5

This is because you've specified the <meta-data> tag in the wrong parent (<application> tag). It should be specified within the <provider> tag. (The two code snippets below show the difference:)

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

VS

<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>
Edric
  • 24,639
  • 13
  • 81
  • 91
  • With this fix, I still get the following error: java.lang.RuntimeException: Unable to get provider androidx.core.content.FileProvider: java.lang.IllegalArgumentException: Missing android.support.FILE_PROVIDER_PATHS meta-data – KijeviGombooc Dec 22 '19 at 12:55
  • I somehow reverted the change and thought I have anothe rproblem, but indeed this was the problem. – KijeviGombooc Dec 22 '19 at 14:52
  • @KijeviGombooc Were you able to resolve this issue ? If yes, please share it. For me even after adding meta-data inside provider, i'm getting this exception. – K Pradeep Kumar Reddy Aug 19 '21 at 09:01
  • To be honest it's been long since I've touched this project, I don't remember how I solved it. My github is the following, feel free to check it: https://github.com/KijeviGombooc/Piros-Fogo – KijeviGombooc Aug 20 '21 at 11:13