Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Get the filename of a File

I thought this would be pretty straight-forward, but can't seem to get this. I have a File file and it has a path file.path which spits out something like /storage/emulated/0/Android/data/my_app/files/Pictures/ca04f332.png but I can't seem to find anything to get just ca04f332.png.

like image 477
Jus10 Avatar asked May 20 '18 22:05

Jus10


People also ask

How do I find the fileName of a file?

To extract filename from the file, we use “GetFileName()” method of “Path” class. This method is used to get the file name and extension of the specified path string. The returned value is null if the file path is null. Syntax: public static string GetFileName (string path);

How do I retrieve files from storage in flutter?

When using Flutter Official path_provider package, getExternalStorageDirectory() will always return path to /storage/emulated/0/your-package-name/files. The plugin ext_storage uses a deprecated version of the Android embedding.


2 Answers

You can use the basename function from the dart path library:

import 'package:path/path.dart';  File file = new File("/dir1/dir2/file.ext"); String basename = basename(file.path); # file.ext 
like image 127
jspcal Avatar answered Sep 20 '22 04:09

jspcal


File file = new File("/storage/emulated/0/Android/data/my_app/files/Pictures/ca04f332.png");  String fileName = file.path.split('/').last;  print(fileName); 

output = ca04f332.png

like image 23
0917237 Avatar answered Sep 24 '22 04:09

0917237