How to get base64 of a file in Flutter - 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;
}
}

Related

How to decode or convert base64 string url to UintList in Dart?

I got this base64Result from canvas.toDataURL() but I having difficulties parsing in Dart to 'UintList'
import 'dart:convert';
final String base64Result = result.toString();
print("$logTrace calling web function done ${base64Result.length}");
final bytes = base64Url.decode(base64Result);
character (at character 5)
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAARwAAAIrCAYAAAA9YyZoAAAgAElEQ...
^
'data:image/png;base64,' is part of the data URL, not part of a base-64 string. You need to extract the base-64 data from the URL first.
Luckily, the UriData class can do this all for you:
final bytes = UriData.parse(base64Result).contentAsBytes();

Converting file path to file in Flutter

I am getting the path of an image from the device storage, like this:
path = res[0]?.path;
print("path:"+path);
The output of that print is:
/data/user/0/qplan/cache/image_picker_b432f88b-3146-4a99-9e8b-acbefd066e3a2471538209034937554.jpeg
I need to convert it to File in order to upload the image to Firestore Storage.
You can get file inserting your path to File object as below;
File fileToUpload = new File(path);
You can use this.
import 'dart:async';
import 'dart:io';
path = res[0]?.path;
File(path).readAsString().then((String contents) {
print(contents);
});

how to encode dart image object with base64

I want to encode an Image object (not from a file) with base64
Eg: this code Image img = Image.memory(base64Decode(decoded(image))); shows how to convert base64 string to Image Object, then how can i reverse this operation ?
Image is a widget, so can't really convert it to base64 string;
Could do it with file:
import 'dart:convert';
import 'dart:io';
....
String base64String = base64Encode(file.readAsBytesSync());
You can use
var image = BASE64.decode(img.toString());
Image.memory(image)

How to get type of file?

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'

Convert base64 to image and save it in temp folder flutter

I have base64 string of image like /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4pLSwzOko+MzZGNywtQFdBRkxOUlNSMj5aYVpQYEpRUk//....
What I want to do is to save this image in temp folder and use that file address for showing image in my app.
How can I do that?
import 'package:path_provider/path_provider.dart' as syspaths;
Decode your base64 string to bytes in memory.
Uint8List bytes = base64.decode(base64String);
Make a temporary directory and file on that directory
final appDir = await syspaths.getTemporaryDirectory();
File file = File('${appDir.path}/sth.jpg');
Write converted bytes on a file
await file.writeAsBytes(bytes)
then we can
Image.file(file);
OR ALTERNATIVELY
Decode your base64 string to bytes in memory.
Uint8List bytes = base64.decode(base64String);
then we can
Image.memory(bytes)