2

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?

Coding Glass
  • 137
  • 1
  • 7

3 Answers3

2

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'
Lee3
  • 2,882
  • 1
  • 11
  • 19
  • I thought this is a bad practice? Shouldn't you access it through some sort of a method? Also, should I just remove the 'file://' part and take the rest of the string? – Coding Glass Dec 17 '20 at 13:16
  • This is not bad practice. It is an *Object* because everything is an *Object* in Dart. It is actually a primitive type in Dart (like *int*, *double*, *bool*, etc). This is how you access its values. And yes, just use *substring* to remove 'file://' if you need to. – Lee3 Dec 17 '20 at 13:43
  • Great, thank you very much for the explanation. I didn't know it is a primitive type either. Hope I'll catch the train soon. Thanks for the patience. – Coding Glass Dec 17 '20 at 19:45
1

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

perymerdeka
  • 766
  • 9
  • 19
0

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);
      }
    }
Tanvir Arafat
  • 41
  • 1
  • 8
  • Thank you for your reply. I was wondering if there is another way, one that doesn't require you to give it your own file name. – Coding Glass Dec 17 '20 at 13:18