Flutter Image gallery saver - how to access path

6.1k views Asked by At

I'm using Image gallery saver plugin for saving images. Method

await ImageGallerySaver.saveImage(pngBytes)

returns an object

{filePath: file:///storage/emulated/0/wallpapers/1608205629471.jpg, errorMessage: null, isSuccess: true}

I'd like to get the path of the newly saved file (/storage/emulated/0/wallpapers/1608205629471.jpg).

Is there a way to achieve that?

3

There are 3 answers

3
Lee3 On BEST ANSWER

That is a Map object. Access the filePath String like so:

var response = await ImageGallerySaver.saveImage(pngBytes);

// value = json['key']
var path = response['filePath']; // 'file:///storage/emulated/0/wallpapers/1608205629471.jpg'
1
Tanvir Arafat On

First You need to same the image id on database that you wants to retrieve.

Then pass the id with this method I am giving a snippet that I have used:

import 'package:path/path.dart' as p;
Directory _appDocsDir;
        class ImageCashUtil {
      ImageCashUtil() {
        init();
      }
      init() async {
        WidgetsFlutterBinding.ensureInitialized();
        _appDocsDir = await getApplicationDocumentsDirectory();
        return _appDocsDir;
      }
    
      File fileFromDocsDir(String filename) {
        String pathName = p.join(_appDocsDir.path, filename);
        return File(pathName);
      }
    }
0
perymerdeka On

you can access the key from a map like this

final imagePath = result['filePath'].toString();

then if you need to get a location path removing file:// with regular expression

final imagePath = result['filePath'].toString().replaceAll(RegExp('file://'), '');

read documentation for more info