androidflutterdart

Clipboard an image in Flutter


how can I copy (clipboard) an image in Flutter? With texts I can but with image I only found these doubts in the Github of Flutter but without solution.

In Text i using with Clipboard.setData(new ClipboardData(text: widget.content)); and working.

But how copy an image?


Solution

  • As @ariefbayu commented, copying files to clipboard is not widely supported, however, as a general standard, some applications follow the rule of copying the path of the file to the clipboard in order to access it.

    To do so, our code could look like this:

    
    Future<String> get _localPath async {
      final directory = await getApplicationDocumentsDirectory();
      return directory.path;
    }
    
    Future<void> _copyFile() async {
       final path = await _localPath;
       Clipboard.setData(new ClipboardData(text: "content://${_localPath}/image.png"));
       return;
    }