How to get type of file? - flutter

I'm trying to find a package which would recognise file type. For example
final path = "/some/path/to/file/file.jpg";
should be recognised as image or
final path = "/some/path/to/file/file.doc";
should be recognised as document

You can make use of the mime package from the Dart team to extract the MIME types from file names:
import 'package:mime/mime.dart';
final mimeType = lookupMimeType('/some/path/to/file/file.jpg'); // 'image/jpeg'
Helper functions
If you want to know whether a file path represents an image, you can create a function like this:
import 'package:mime/mime.dart';
bool isImage(String path) {
final mimeType = lookupMimeType(path);
return mimeType.startsWith('image/');
}
Likewise, if you want to know if a path represents a document, you can write a function like this:
import 'package:mime/mime.dart';
bool isDocument(String path) {
final mimeType = lookupMimeType(path);
return mimeType == 'application/msword';
}
You can find lists of MIME types at IANA or look at the extension map in the mime package.
From file headers
With the mime package, you can even check against header bytes of a file:
final mimeType = lookupMimeType('image_without_extension', headerBytes: [0xFF, 0xD8]); // jpeg

There is no need of any extension. You can try below code snippet.
String getFileExtension(String fileName) {
return "." + fileName.split('.').last;
}

If think you should take a look to path package, specially to extension method.
You can get file format without adding one more package to pubspec.yaml ;)
context.extension('foo.bar.dart.js', 2); // -> '.dart.js
context.extension('foo.bar.dart.js', 3); // -> '.bar.dart.js'
context.extension('foo.bar.dart.js', 10); // -> '.bar.dart.js'
context.extension('path/to/foo.bar.dart.js', 2); // -> '.dart.js'

Related

Flutter how to remove file extension when i need to put file name into variable

I use p.basename from flutter path library to get file name, but i got it with extension, how to remove extension? also to get path I use getApplicationDocumentsDerectory from path_provider library
File newFile = File(filepath);
String name = p.basename(newFile.path);
this is code I use to get file name
You just use 'basenameWithoutExtension' method like basename.
File newFile = File(filepath);
String name = p.basename(newFile.path);
String nameWithoutExtension = p.basenameWithoutExtension(newFile.path);

How to get base64 of a file in Flutter

I have an file path string like
path = /data/user/0/com.digitalpathshalabd.school/cache/Shaiful_Islam.docx
now I want to convert the file into base64
How could I achieve this ?
Finally I come up with a solution.
The thing is we have to get the actual file from path before converting it.
get the actual file
convert the file into byte array
finaly convert the byte array to base64
import 'dart:convert';
import 'dart:io';
class FileConverter {
static String getBase64FormateFile(String path) {
File file = File(path);
print('File is = ' + file.toString());
List<int> fileInByte = file.readAsBytesSync();
String fileInBase64 = base64Encode(fileInByte);
return fileInBase64;
}
}

Flutter get file name

String fileName = _profPic.path.split('/').last;
print(fileName);
Output is Screenshot_2020-05-12-17-14-07-564_com.miui.home.png
but I require only home.png
It's impossible path or dart can decide which parts of the name you need.
You must manipulate the string. In your case, this method will do what you need:
String getName(String fullName){
final parts = fullName.split('.');
return parts.skip(parts.length - 2).take(2).join('.');
}
example:
final name = 'Screenshot_2020-05-12-17-14-07-564_com.miui.home.png';
print(getName(name)); // home.png
Or, you can convert this method into an extension.

File cant be assigned with ImagePicker Flutter

i am learning imagepicker from https://pub.dev/packages/image_picker ,
but i don't why i got an error when i have use the way step by step.
this is the problem :
first, i declare a File variable
File _imageFile ;
then i use it in a method,
_getimg() async{
var _img = await ImagePicker(source: ImageSource.gallery);
setState(() {
_imageFile = _img ;
});
}
and then i got this error :
A value of type 'File (where File is defined in
D:\Flutter\flutter\bin\cache\pkg\sky_engine\lib\io\file.dart)' can't
be assigned to a variable of type 'File (where File is defined in
D:\Flutter\flutter\bin\cache\pkg\sky_engine\lib\html\html_dart2js.dart)'.
There is a conflict between Files declaration. The html package has one declaration of a class File and the io package has another declaration (same name, different origin).
In fact, using html is for web and io is used for console, server or mobile apps, so check your imports and delete io or html depending in the type of project you are working on.
Another solution is to define your imports like this:
import 'package:html/html.dart' as h; //"h" can change, is just an example
import 'dart:io' as i; //"i" also can be another char or word, is just an example
//And when you need to create a File,
//you can decide if you want to create
//an io File or an html File
main(){
i.File f1 = ...; //The io File, starting with "i."
h.File f2 = ...; //The html File, starting with "h."
}

cq5 determine file content type or media type or mimetype

I have following code to save an asset in a servler service
ResourceResolver resourceResolver = this.resolverFactory.getAdministrativeResourceResolver(null);
AssetManager assetMgr = (AssetManager)resourceResolver.adaptTo((Class)AssetManager.class);
String newFile = "/content/dam/travel/" + fileName;
assetMgr.createAsset(newFile, is, "image/jpeg", true);
The following line creates the asset with specified mimetype
assetMgr.createAsset(newFile, is, "image/jpeg", true);
I would like to determine the coming file content type based on stream rather than name.
Thank you,
Sri