FileProvider - IllegalArgumentException: Konfiguriertes Stammverzeichnis konnte nicht gefunden werden
Ich versuche, ein Bild mit der Kamera zu machen, aber ich bekomme folgende Fehlermeldung:
FATAL EXCEPTION: main
Process: com.example.marek.myapplication, PID: 6747
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.marek.myapplication/files/Pictures/JPEG_20170228_175633_470124220.jpg
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
at com.example.marek.myapplication.MainActivity.dispatchTakePictureIntent(MainActivity.java:56)
at com.example.marek.myapplication.MainActivity.access$100(MainActivity.java:22)
at com.example.marek.myapplication.MainActivity$1.onClick(MainActivity.java:35)
AndroidManifest.xml:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.marek.myapplication.fileprovider"
android:enabled="true"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
Java:
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) {
Toast.makeText(getApplicationContext(), "Error while saving picture.", Toast.LENGTH_LONG).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.marek.myapplication.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path name="my_images" path="images/"/>
</paths>
Ich habe den ganzen Tag über diesen Fehler gesucht und versucht, FileProvider
zu verstehen, aber ich habe keine Ahnung, was diese Fehlermeldung mir sagen will. Wenn Sie mehr Infos/Code wollen, schreiben Sie mir in den Kommentar.
173
3
Ihre Datei ist unter
getExternalFilesDir()
gespeichert. Das entspricht dem `Das verwirrt mich auch ein wenig.
Das Problem ist auf "path" Attribut in Ihrer Xml-Datei.
Aus diesem Dokument FileProvider 'path' ist ein Unterverzeichnis, aber in einem anderen Dokument (camera/photobasics) zeigt 'path' ist ein vollständiger Pfad.
Ich ändere einfach diesen 'path' in den vollen Pfad und es funktioniert einfach.
Prüfen Sie, wie viele Speicherplätze Ihr Gerät bietet - die gemeinsame Nutzung von Dateien aus dem sekundären Speicher wird nicht unterstützt. Sehen Sie sich den Quelltext von
FileProvider.java
an (aus support-core-utils 25.3.1):Also, sie nehmen nur den ersten Speicherplatz.
Sie können auch sehen, dass
getExternalCacheDirs()
verwendet wird, um eine Liste von Speichern über die SchnittstelleContextCompat
zu erhalten. Siehe Dokumentation für seine Grenzen (es'wird gesagt, dass es zum Beispiel keine USB-Flashes erkennt). Am besten ist es, wenn Sie selbst eine Debug-Ausgabe der Liste der Speicher aus dieser API erstellen, so dass Sie überprüfen können, ob der Pfad zum Speicher mit dem Pfad übereinstimmt, der angetUriForFile()
übergeben wurde.Es gibt bereits ein ticket assigned (Stand 06-2017) im Google's issue tracker, mit der Bitte, mehr als einen Speicher zu unterstützen. Schließlich fand ich auch SO Frage zu diesem.