skip to Main Content

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


  1. The location of your file does not match the paths in the xml file.

    Realise that you app has two cache locations.

    getCacheDir() and getExternalCacheDir()
    

    You mixed them up.

    All is in: https://developer.android.com/reference/androidx/core/content/FileProvider

    Login or Signup to reply.
  2. To overcome this issue you can configure your URL in following way:

    String imageUrl =
      "https://t4.ftcdn.net/jpg/01/43/23/83/360_F_143238306_lh0ap42wgot36y44WybfQpvsJB5A1CHc.jpg";
    
    // 1. Download the image to a temporary file
    final response = await http.get(Uri.parse(imageUrl));
    final bytes = response.bodyBytes;
    final temp = await getTemporaryDirectory();
    final path = '${temp.path}/image.jpg';
    final file = File(path);
    await file.writeAsBytes(bytes);
    
    // 2. Share the temporary file using shareXFiles
    await Share.shareFiles([path], text: 'Image Shared');
    

    I hope this helps.
    Happy Fluttering!! 💙

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search