Flutter share image intent

1,514

In the end, none of the proposed solutions did not work; neither using extenal-path, nor the other solution.

The way how I managed in the end to share file was to actually copy it to a temporary (cache) directory first (in flutter path_provider it's getTemporaryDirectory()) and update file_paths.xml to following:

<cache-path name="image" path="."/>

and finally under MainActivity.java:

Uri uri = FileProvider.getUriForFile(this, "com.example.android.fileprovider", new File(this.getCacheDir(), path));
Share:
1,514
Robert J.
Author by

Robert J.

Updated on December 07, 2022

Comments

  • Robert J.
    Robert J. over 1 year

    I am trying to share an image via classic Intent. I have added the following items:

    file_paths.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <paths>
        <files-path name="my_images" path="." />
    </paths>
    

    Manifest:

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.android.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
    

    And finally MainActivity.java:

    private void shareFile(String fileName) {
            Intent share = new Intent(Intent.ACTION_SEND);
            Uri uri = FileProvider.getUriForFile(this, "com.example.android.fileprovider", new File(this.getApplicationInfo().dataDir + "/app_flutter/userphotos", fileName));
            share.setData(uri);
            share.setType("image/png");
            share.putExtra(Intent.EXTRA_STREAM, uri);
            share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(Intent.createChooser(share, "Share"));
        }
    

    The problem I am facing is that image I am trying to share has the following path:

    /data/user/0/shoedrobe.innovativeproposals.com.shoedrobe/app_flutter/userphotos/receipt20181101094430.jpg

    However the FileProvider is trying to access it from here:

    java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/shoedrobe.innovativeproposals.com.shoedrobe/app_flutter/userphotos/receipt20181101094430.jpg

    For saving images I am using package path_provider and I am saving items under getApplicationDocumentsDirectory(), which in Android is AppData directory.

    I am not sure why FileProvider decided suddenly to go from /data/user/0/ to /data/data/ folder, therefore any help or tips regarding this matter would be highly appreciated.

    Update:
    I have updated the code as per recommendations and replaced the Uri under MainActivity.java with the following line:

    Uri uri = FileProvider.getUriForFile(this, "com.example.android.fileprovider", new File(this.getDir("flutter", Context.MODE_PRIVATE).getPath() + "/app_flutter/userphotos", path));
    

    Nonetheless, the problem still persists (same exception that file is supposed to be under /data/data/<package> instead of /data/user/0. I have also tried to add additional persmissions to my AndroidManifest:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    

    but it did not work as well. Could the problem lie with my file_paths.xml file?