0

I am saving .txt files in a subdirectory named "SaveFiles" under files in my applications folder. At runtime I would like to share certain files to through WhatsApp/discord/email as attachments.

When I try to share the file the chooser opens and if I select WhatsApp I'm able to see the contact and when I click on the contact I wish to share with I get a Toast message saying "Sharing Failed, please try again".

This is my provider

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

This is my res/xml/file_paths.xml

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="all" path="." />
    <files-path name="SaveFiles" path="SaveFiles/" />
    <files-path name="Images" path="Images/" />
</paths>

And this is the code I'm using to share the file

    public  void  ShareFile(String fileName){
        try {
            File imagePath = new File(getFilesDir(), "SaveFiles");
            File file = new File(imagePath,fileName);

            Uri uri = FileProvider.getUriForFile(this, "com.MyCompany.fileprovider", file);
            Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
            shareIntent.setType("text/plain");
            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(Intent.createChooser(shareIntent, "Share File"));
        }catch (Exception e){
            Log.d(TAG, "ShareFile: " + e.getMessage());
            ShowMessage(e.getMessage());
        }

    }

Any help would be appreciated. Thanks.

0 Answers0