Für Anwendungen Mit Android 7.0 erzwingt das Android-Framework die API-Richtlinie StrictMode
, die das Freigeben von file://
URIs außerhalb Ihrer App verbietet. Wenn eine Absicht, die einen Datei-URI enthält, Ihre App verlässt, schlägt die App mit der Ausnahme FileUriExposedException
fehl.
Um Dateien zwischen Anwendungen zu teilen, sollten Sie einen Inhalt senden: // URI und gewähren Sie eine temporäre Zugriffsberechtigung für den URI. Die einfachste Möglichkeit, diese Berechtigung zu erteilen, besteht in der Verwendung der Klasse FileProvider
.
Sie meine Lösung Versuchen können ..
1.Add res/xml/provider_paths.xml
provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="images" path="."/>
</paths>
2.Add in Manifest innerhalb tag
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="android3.maxtingapp.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
3.in Ihre Aktivität hinzufügen Funktion für Crop Bild wie unten meine Probe
private void cropImage(File file) {
final int width = 400;
final int height = 200;
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
Uri contentUri;
if(Build.VERSION.SDK_INT > M){
contentUri = FileProvider.getUriForFile(AddPlace.this,
"android3.maxtingapp.provider",
file);//package.provider
//TODO: Permission..
getApplicationContext().grantUriPermission("com.android.camera",
contentUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
cropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
cropIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}else{
contentUri = Uri.fromFile(file);
}
cropIntent.setDataAndType(contentUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 2);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", width);
cropIntent.putExtra("outputY", height);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, REQUEST_CROP_ICON);
}catch (ActivityNotFoundException a) {
Log.e("Activity Not Found",""+a.toString());
}
}
Ich hoffe, das wäre hilfreich für someOne ..
das ist nicht Ernte Problem. Es gab Änderungen im Dateizugriffssystem in Android 7. Sie müssen Ihren Inhaltsanbieter von nun an implementieren, um anderen Apps zu ermöglichen, Ihre bereitgestellte Uris –
'exposed beyond app über ClipData.Item.getUri()' zu verwenden. Ich frage mich, wie das aussehen würde möglich. – greenapps
https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed – CommonsWare