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)
Related
I have used filepicker to choose file from internal storage:
Fresult = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['pdf', 'jpg'],
PlatformFile file = Fresult.files.first;
fiLeName=file.name.toString();
fiLePath=file.path.toString();
);
and to convert that file suppose a pdf to base64 I used
final bytes = io.File(fiLePath).readAsBytesSync();
String vbase= base64Encode(bytes);
When i print the base64 string which i have received and used an online converter to see if the file is corrupted or not. but fortunately the result is corrupted.
Is there any way to do it?
or have I done it wrong?
please help me with it
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;
}
}
Is there any way to read just a chunk of bytes from a file in flutter not the whole data?
For example from byte 50 to 150
I found the solution myself:
Directory directory = await getApplicationDocumentsDirectory();
File file = File('${directory.path}/myfile.txt');
RandomAccessFile raf= file.openSync(mode: FileMode.read);
raf.setPositionSync(50);
Uint8List data = raf.readSync(100);
How can i covert Image file to binary data ?
I'm using a library call image_picker for pick the image from gallery or camera.
And I want to convert the image that I picked to binary data.
File image = await ImagePicker.pickImage(source: ImageSource.gallery)
(image as Image).toByteData // the method toByteData here is not pop up.
toByteData() method allows converting the image into a byte array. We need to pass the format in the format argument which specifies the format in which the bytes will be returned. It'll return the future that completes with binary data or error.
final pngByteData = await image.toByteData(format: ImageByteFormat.png);
ImageByteFormat enum contains the following constants.
png
rawRgba
rawUnmodified
values
For more information about ImageByteFormat, please have a look at this documentation.
Update : If you want to convert the image file into bytes. Then use readAsByte() method.
var bytes = await ImagePicker.pickImage(source: ImageSource.gallery).readAsBytes();
For converting image into a file, Check out this answer.
simply use this method
var bytes = await File('filename').readAsBytes();
You might want to consider using the toByteData method.
It converts an image object into an array of bytes.
It returns a future which returns a binary image data or an error if the encoding fails.
Here is the implementation from this website: https://api.flutter.dev/flutter/dart-ui/Image/toByteData.html
Future<ByteData> toByteData({ImageByteFormat format = ImageByteFormat.rawRgba}) {
return _futurize((_Callback<ByteData> callback) {
return _toByteData(format.index, (Uint8List encoded) {
callback(encoded?.buffer?.asByteData());
});
});
}
The format argument specifies in which encoding format the bytes will be returned in.
Hope this helps!
i am using following code to encode PDF file to base 64
File f = new File(_path);
// String base64pdf = base64Encode(f.readAsBytesSync());
String base64pdf = base64.encode(f.readAsBytesSync());
String fileName = _fileName;
print('base64pdf: '+base64pdf);
I tried to encode the pdf file on here: encoding website
the result from the website shows that the base64 encoder inside flutter(dart) is trimming a big section of the String Generated and give only little first part of it ?
how to solve that , is this related with byte type?
RaisedButton(
onPressed: () async {
List<int> imageBytes = await sampleImage.readAsBytes();
base64Image = base64Encode(imageBytes);
print(base64Image);},),
SizedBox(height: 30,),
Image.memory(base64Decode(base64Image)),
Base 64 code in my git hub repo