FileProvider ne fonctionne Pas avec l'Appareil-photo
Je suis en train de faire Application Appareil photo pour stocker le résultat de mon mémoire de stockage interne. Je comprends aussi que les applications tierces ne sont pas en mesure d'accéder à la mémoire Interne de mon application MAIS nous sommes en mesure de le faire en exposant le répertoire interne par le biais de FileProvider
. J'ai suivi le guide ici:
- Caméra Intention de ne pas sauver de la photo
- https://developer.android.com/reference/android/support/v4/content/FileProvider.html
- Je indiquer mon AndroidManifest.xml
de la manière suivante:
<?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>
J'ai créé un fichier xml nommé file_provider_path.xml
dans /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>
C'est ainsi que j'ai créer et appeler la Caméra Intention:
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);
Quand je lance l'application, je peux obtenir les ci-dessous Logcat résultat:
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
Au moment où la caméra se ferme, j'ai frappé à la ci-dessous Exception:
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)
N'importe qui peut aider à expliquer pourquoi le problème se produit-il? J'ai créé un exemple de projet de mon problème: http://www.axzae.com/downloads/CameraWithFileProvider.zip
De toute façon, l'application ne fonctionnera que si je spécifier explicitement:
context.grantUriPermission(
"com.google.android.GoogleCamera",
imageUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION
);
Mais cela ne fonctionne que pour nexus/cyanogen appareils. Appareil photo Samsung application peut utiliser un autre nom de package.
Vous devez vous connecter pour publier un commentaire.
S'il vous plaît ne pas essayer de mettre
exported="true"
comme l'a soulignépskink
, votre application va se planter au moment du chargement. FileProvider n'est pas destinée à travailler dans cet état.Essayé
intent.addFlags
solution parCommonsWare
. Pas de travail. probablement ne fonctionne qu'avecACTION_SEND
genre d'intention.J'ai trouvé la réponse dans ce post. Apparemment, le manuel de l'octroi de manière est la seule solution. Mais nous pouvons toujours en boucle à travers la liste des candidats à la forfaits et grantPermission à tous.
addFlags()
fonctionne sur Android 5.0+. Sur les appareils plus anciens, votre approche est nécessaire. Voir github.com/commonsguy/cw-omnibus/tree/master/Camera/... pour un exemple.intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
a fonctionné pour moi sur Android SDK 18 (version 4.3 Jellybean).addFlags()
est suffisante pour Android 5.0+.addFlags()
donc c'est probablement pas besoin de plus de temps pour les dernières versions d'Android.J'ai eu un problème similaire, mais plus tard, je trouve une faute de frappe dans mon code.
La typo a été en AndroidManifest.xml précisément dans la balise à la ligne "autorités".
Pour moi,
ne fonctionne pas (quand je l'appelle la caméra instance, l'application se bloque) mais si je supprime "fichier" et remplacez la chaîne comme suit:
mon appli fonctionne bien (l'app prend la photo, les stocke, et donne un aperçu de l'activité dans l'application cliente).
Votre app ont également le même problème ?