FileProvider arbeitet nicht mit der Kamera
Ich versuche zu machen, die Kamera-App zu speichern, die Ausgabe auf meinem internen Speicher. Ich verstehe auch, dass Drittanbieter-apps sind nicht in der Lage, Zugriff auf den Internen Speicher meiner Anwendung ABER sind wir in der Lage, so zu tun, indem Sie das interne Verzeichnis durch FileProvider
. Ich habe die Anleitung hier:
- Kamera mit Absicht nicht speichern Foto
- https://developer.android.com/reference/android/support/v4/content/FileProvider.html
Angeben ich meine AndroidManifest.xml
in der folgenden Weise:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stackoverflow.test.camerawithfileprovider" >
<application>
<activity
...
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.stackoverflow.test.camerawithfileprovider.fileprovider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_path" />
</provider>
</application>
</manifest>
Erstellte ich eine xml-Datei mit dem Namen file_provider_path.xml
im /res/xml/
:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="image_capture" path="public/" />
</paths>
Dies ist, wie ich erstellen und rufen Sie die Kamera-Vorsatz:
private static final int CAMERA_REQUEST_CODE = 5000;
private static final String CAMERA_FP_AUTHORITY = "com.stackoverflow.test.camerawithfileprovider.fileprovider";
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Context context = MainActivity.this;
File imagePath = new File(context.getFilesDir(), "public");
if (!imagePath.exists()) imagePath.mkdirs();
File newFile = new File(imagePath, "tmp.jpg");
Uri imageUri = FileProvider.getUriForFile(context, CAMERA_FP_AUTHORITY, newFile);
//context.grantUriPermission("com.google.android.GoogleCamera", imageUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
Log.d("YouQi", "Image URI Passing to Camera: " + imageUri.toString());
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
Wenn ich die Anwendung ausführen, bekomme ich die unten Logcat Ergebnis:
11-11 20:07:39.581 18303-18303/com.stackoverflow.test.camerawithfileprovider D/YouQi: Image URI Passing to Camera: content://com.stackoverflow.test.camerawithfileprovider.fileprovider/image_capture/tmp.jpg
In dem moment, wenn die Kamera schließt sich, ich drücke die unten-Ausnahme:
com.google.android.GoogleCamera E/AndroidRuntime: FATAL EXCEPTION: main
com.google.android.GoogleCamera E/AndroidRuntime: Process: com.google.android.GoogleCamera, PID: 19420
com.google.android.GoogleCamera E/AndroidRuntime: java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{42e44f70 19420:com.google.android.GoogleCamera/u0a83} (pid=19420, uid=10083) that is not exported from uid 10312
com.google.android.GoogleCamera E/AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1465)
com.google.android.GoogleCamera E/AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1419)
com.google.android.GoogleCamera E/AndroidRuntime: at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:2882)
com.google.android.GoogleCamera E/AndroidRuntime: at android.app.ActivityThread.acquireProvider(ActivityThread.java:4544)
com.google.android.GoogleCamera E/AndroidRuntime: at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2274)
com.google.android.GoogleCamera E/AndroidRuntime: at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1425)
com.google.android.GoogleCamera E/AndroidRuntime: at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:906)
com.google.android.GoogleCamera E/AndroidRuntime: at android.content.ContentResolver.openOutputStream(ContentResolver.java:669)
com.google.android.GoogleCamera E/AndroidRuntime: at android.content.ContentResolver.openOutputStream(ContentResolver.java:645)
com.google.android.GoogleCamera E/AndroidRuntime: at com.android.camera.PhotoModule.onCaptureDone(PhotoModule.java:1281)
com.google.android.GoogleCamera E/AndroidRuntime: at com.android.camera.PhotoModule$8.onClick(PhotoModule.java:593)
com.google.android.GoogleCamera E/AndroidRuntime: at android.view.View.performClick(View.java:4445)
com.google.android.GoogleCamera E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:18446)
com.google.android.GoogleCamera E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:733)
com.google.android.GoogleCamera E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
com.google.android.GoogleCamera E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)
com.google.android.GoogleCamera E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5146)
com.google.android.GoogleCamera E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
com.google.android.GoogleCamera E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)
com.google.android.GoogleCamera E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
com.google.android.GoogleCamera E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
com.google.android.GoogleCamera E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
Kann mir jemand helfen zu erklären, warum das problem passiert? Erstellt habe ich ein Beispielprojekt mein problem: http://www.axzae.com/downloads/CameraWithFileProvider.zip
Sowieso, die app wird funktionieren, wenn ich explizit angeben:
context.grantUriPermission(
"com.google.android.GoogleCamera",
imageUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION
);
Aber dies funktioniert nur für nexus/cyanogen-Geräte. Samsung Kamera-app möglicherweise verwenden Sie einen anderen Paket-Namen.
InformationsquelleAutor der Frage You Qi | 2015-11-11
Du musst angemeldet sein, um einen Kommentar abzugeben.
Bitte nicht versuchen
exported="true"
wie bereits vonpskink
dem app-Absturz der moment, den es lädt. FileProvider ist nicht vorgesehen, die arbeiten in diesem Zustand.Versucht
intent.addFlags
Lösung vonCommonsWare
. Funktioniert nicht. wahrscheinlich funktioniert nur mitACTION_SEND
Art des Vorsatzes.Fand ich die Antwort in diesem Beitrag. Offenbar ist die manuelle Vergabe Art und Weise ist die einzige Lösung. Aber wir können immer eine loop-thru-die Liste der Kandidaten-Pakete und grantPermission Sie alle von Ihnen.
InformationsquelleAutor der Antwort You Qi
Ich hatte ein ähnliches Problem, aber später, finde ich einen Tippfehler in meinem code.
Der Tippfehler war in AndroidManifest.xml genau in die-tag in der Zeile "Behörden".
Für mich,
funktioniert nicht (wenn ich die Kamera Instanz, die app stürzt ab), aber wenn ich " löschen "Datei", und ersetzen Sie diese Zeichenfolge wie folgt:
meine app läuft reibungslos (die app nimmt das Foto, speichert es und gibt wieder eine Vorschau auf die Tätigkeit in der client-app).
Bietet Ihre app habe auch das gleiche Problem ?
InformationsquelleAutor der Antwort Alessandro Iudicone