I am not able to save a widget screenshot to gallery in flutter

Rajendra A Verma

error showing : I/flutter (24734): {filePath: null, errorMessage: java.io.FileNotFoundException: /storage/emulated/0/Pictures/1622347717438.jpg: open failed: EACCES (Permission denied), isSuccess: false}

problem : not saving screenshot of widget in gallery.

here is full code :

import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Save image to gallery',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  GlobalKey _globalKey = GlobalKey();

  @override
  void initState() {
    super.initState();

    _requestPermission();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Save image to gallery"),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
              RepaintBoundary(
                key: _globalKey,
                child: Container(
                  width: 200,
                  height: 200,
                  color: Colors.red,
                ),
              ),
              Container(
                padding: EdgeInsets.only(top: 15),
                child: RaisedButton(
                  onPressed: _saveScreen,
                  child: Text("Save Local Image"),
                ),
                width: 200,
                height: 44,
              ),
            ],
          ),
        ));
  }

  _requestPermission() async {
    Map<Permission, PermissionStatus> statuses = await [
      Permission.storage,
    ].request();

    final info = statuses[Permission.storage].toString();
    print(info);
    _toastInfo(info);
  }

  _saveScreen() async {
    RenderRepaintBoundary boundary =
        _globalKey.currentContext!.findRenderObject() as RenderRepaintBoundary;
    ui.Image image = await boundary.toImage();
    ByteData? byteData = await (image.toByteData(format: ui.ImageByteFormat.png)
        as FutureOr<ByteData?>);
    if (byteData != null) {
      final result =
          await ImageGallerySaver.saveImage(byteData.buffer.asUint8List());
      print(result);
      _toastInfo(result.toString());
    }
  }

  _toastInfo(String info) {
    Fluttertoast.showToast(msg: info, toastLength: Toast.LENGTH_LONG);
  }
}

Please Help me I am using my Android mobile for debugging app not emulator. and firstly mobile give permission to access media file but when I click button it showing error error while clicking a button

Rushikesh Talokar

If you are running it on Android, add WRITE_EXTERNAL_STORAGE permission in <your_project>\android\app\src\main\AndroidManifest.xml

Example:

<manifest>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    ...
    <application>
        ...
        <activity> 
            ...
        </activity>
    </application>
</manifest> 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Unity Android save screenshot in gallery

I want to use checkboxes and sorting feature of the DataTable widget at the same time in flutter. I am only able to use any one

How can I store widget screenshot as jpg file in flutter?

I am trying to wrap my Column widget in a SingleChildScrollView but not able to

I am able to save data to Firebase but i can't retrieve it

How can I take a partial screenshot of my Xamarin.Forms app and save it into my gallery?

Flutter save Image in Gallery folder

Using screenshot_and_open_image method from capybara-screenshot gem; I want to be able to specify what file name to save by

How am I able to pass setState in a stateless widget from a statefull widget

I have an image with a draggable text widget over it. How can I save this image with the text to the gallery?

How to save an image permanently on Flutter after picking from Gallery or Camera? Am getting this error when am setting the picked image

I am not able to login

I am using flutter bottomNavigatonBar widget, Is there anything which it can be raised?

How to take screenshot of widget beyond the screen in flutter?

Set wallpaper from widget screenshot in flutter

How do I save data from a stateful widget in Flutter?

Why am I not able to save my nested attributes when I'm using accepts_nested_attributes_for

How to save an image to the photo gallery using Flutter?

How to save video to phone gallery - Dio/Flutter

I am not able to save user address,browser or time it always say syntax error

Not able to save screenshot in klov reporter. (Extent Reports)

I am not able to use ipfs

I am not able to install svnnotify

I am not able to use the variables

I am new to flutter and i think i have misunderstood the widget and layout system

Why am I not able to install flutter on my 32 bit windows 10 machine?

How can I use ffmpeg to output a screenshot gallery / mosaic?

I am getting Key error message while trying to save my Django form. I am not able to save data from Django form to the database

I want to hide one widget and display other widget it is possible in home page where i am calling them but not possible in drawer in flutter