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);
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 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)
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
I am trying to convert a Uint8List to a string using Dart (in a Flutter project).
I am using the Flutter Android USB Serial plugin ( https://github.com/altera2015/usbserial)
The data are coming from a usb device and are returned from the library as a Stream.
If outputed as a string, it looks like:
[255,0,0,0,255....]
When I try:
String newTag = ascii.decode(asyncSnapshot.data);
I get the error :
FormatException:Invalid value in input: 255
I don't know how to solve this issue, my result should be :
"352206000079439"
Try this one;
List<int> list = 'someData'.codeUnits;
Uint8List bytes = Uint8List.fromList(list);
String string = String.fromCharCodes(bytes);
If data has compressed as a blob type.
Uint8List bytes = Uint8List.fromList(tcp_socket_blob_data);
var inflated = zlib.decode(bytes);
var data = utf8.decode(inflated);
More: flutter/dart: How to decompress/inflate zlib binary string in flutter
I'm able to upload file using below code
var fileToUpload = 'tt.jpg';
var absolutePath = path.resolve(__dirname, fileToUpload);
var FileDetector = require('selenium-webdriver/remote/index.js').FileDetector;
browser.setFileDetector(new FileDetector());
element(by.css('input[type="file"]')).sendKeys(absolutePath);`
But file will upload with zero byte size even though file size is actually 1 KB
Fileupload successfully but with 0 size bytes here
My development added below code for uploading the file
Dev Code