Flutter - sharing files from application's document directory gives IllegalArguementException

1.2k Views Asked by At

I am trying to share a file using SharePlus. However, I get the following error:

The task is to generate a PDF file and share it through ios and android. I see that share_plus is able to share the file. However, I am unable to share by saving it in the applicationDocumentDirectory.

Update: I have attached a demo repo with codes from below. The error is showing in my app even when the repo is working without permissions fine. share_plus complains with below issues.

Repo: Link

Error:

Unhandled Exception: PlatformException(error, Failed to find configured root that contains /data/user/0/com.example.flutter_share_demo/app_flutter/someRandom.pdf, null, java.lang.IllegalArgumentException: Failed to find configured root that contains /data/user/0/com.example.flutter_share_demo/app_flutter/someRandom.pdf

The code is as follows:

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:permission_handler/permission_handler.dart';
import 'package:share_plus/share_plus.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('Share files')),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Center(
              child: TextButton(
                child: const Text('Generate and Share PDF'),
                onPressed: () async {
                  final pdf = pw.Document();
                  pdf.addPage(pw.Page(
                    build: (context) =>
                        pw.Center(child: pw.Text('Hello, World!')),
                  ));

                  final Directory storageDir = getApplicationDocumentDirectory();

                  try {     
                    
                      File file = File('${storageDir.path}/someRandom.pdf');

                      await file.writeAsBytes(await pdf.save());

                      print(file.path);
                      print('File exists: ${await file.exists()}');

                      Share.shareFiles([file.path], subject: 'Shared file');
                    } else {
                      ScaffoldMessenger.of(context).showSnackBar(
                          const SnackBar(content: Text('Permission denied')));
                    }
                  } on PlatformException catch (ex) {
                    print(ex);
                  } catch (ex) {
                    print(ex);
                  }
                },
              ),
            ),
          ],
        ));
  }
}

0

There are 0 best solutions below