Monday, 12 November 2018

Flutter share image intent

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.



from Flutter share image intent

No comments:

Post a Comment