I want to share an Image from a url. I am using the share_plus
and shareFiles
method to share it.
Please check my code here:
final url = Uri.parse(url);
final response = await http.get(url);
final bytes = response.bodyBytes;
final directory = await getTemporaryDirectory();
final file = File('${directory.path}/tempp.png');
await file.writeAsBytes(bytes);
print(file.path);
await Share.shareFiles([file.path]);
I’m getting this exception
PlatformException (PlatformException(error, Failed to find configured root that contains /storage/emulated/0/Android/data/mrblab.news_app/cache/share/tempp.png, null, java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/mrblab.news_app/cache/share/tempp.png
at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:825)
at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:450)
at io.flutter.plugins.share.Share.getUrisForPaths(Share.java:128)
at io.flutter.plugins.share.Share.shareFiles(Share.java:69)
at io.flutter.plugins.share.MethodCallHandler.onMethodCall(MethodCallHandler.java:42)
at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:258)
at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322)
at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
at android.os.Handler.handleCallback(Handler.java:984)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loopOnce(Looper.java:238)
at android.os.Looper.loop(Looper.java:357)
at android.app.ActivityThread.main(ActivityThread.java:8149)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:957)
))
I have given READ
and WRITE
permissions in AndroidManifest.xml
.
I’ve also created provider_paths.xml
after seeing this post on StackOverflow
<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path
name="share"
path="." />
</paths>
How do I fix it?
2
Answers
The location of your file does not match the paths in the xml file.
Realise that you app has two cache locations.
You mixed them up.
All is in: https://developer.android.com/reference/androidx/core/content/FileProvider
To overcome this issue you can configure your URL in following way:
I hope this helps.
Happy Fluttering!! 💙