I'm trying to crop an image, I used ImageCropper().
CroppedFile? croppedFile = await ImageCropper().cropImage(sourcePath: file.path);
How to convert this croppedFile into File?
Use the path property of the CroppedFile class to give you the file path as a string. Convert this path to a file type using File(), like so...
yourFile = File(croppedFile!.path);
Related
I copied some code from the web and I had these lines
File image;
...
image.writeAsBytesSync(result);
But I had to change image type to XFile. Is there a way to do the same operation with XFile class?
A simple work-around like this will work:
XFile image;
...
File file = File(image.path);
file.writeAsBytesSync(result);
image = XFile(file.path);
I'm trying with two different images with same name. But the path was same for two picked images. it was not unique.So that I uploaded in server second image with the same name of first uploaded image. But server had both the image are same and it had first image. So how to handle this case and customize the path?
You can use the path_provider to define the customize directory on your app.
So, copy the file with your customize path and rename the file name.
BTW, do NOT save the absolute path of File on iOS.
The iOS use SandBox to access the file. When you get the file path every time. The file path will be different.
class FileUtils {
final String avatarPath = '/avatar/';
Future<String> getAvatarDirectoryPath() async {
final String appDirPath = await getApplicationSupportDirectory().path;
final Directory avatarDirPath = await Directory(appDirPath + avatarPath).create();
return directory.path;
}
}
// Example
{
final XFile? image = await ImagePicker().pickImage(source: ImageSource.camera);
final File imageFile = File(image.path);
final File newFile = File(await FileUtils().getAvatarDirectoryPath() + 'userAvatar.png');
await imageFile.copy(newFile.path);
}
I've tried this way, and use it in FileImage but haven't found a rally point
Uint8List bytes = base64.decode(imageBase64);
File file = File.fromRawPath(bytes);
imageBase64 is the image url that has been encode.
The point of this thing is, i want to use it in FileImage or another widget image file but i don't want this image is saved to device and upload this file to the attachments file Rest API
can anyone help me.
If what you want to do once you have the bytes is to show the image, then you can use Image.memory.
var imageWidget = Image.memory(bytes);
OTOH If you're trying to upload the image, you don't need to convert it into file. You can use bytes directly assuming you can't upload it using base64 enconding. Package http
import 'package:http/http.dart';
...
//create multipart using filepath, string or bytes
MultipartFile multipartFile = MultipartFile.fromBytes(
'file',
byteData,
filename: fileName,
);
//add multipart to request
request.files.add(multipartFile);
var response = await request.send();
If you have an UintList8 you can use MemoryImage(bytes).
Uint8List bytes = base64.decode(imageBase64);
Image(image: MemoryImage(bytes));
I am using MultipartFile to send my image file to server. I know that when I use ImagePicker
File ImageFile;
_openCamera() async {
var picture = await ImagePicker.pickImage(source: ImageSource.camera);
this.setState(() {
imageFile = picture;
});
}
then use MultipartFile like this
request.files.add(http.MultipartFile.fromBytes('img', ImageFile.readAsBytesSync(), filename: 'photo.jpg'));
But my problem is I want my image is from my assets like from here Image.asset('images/photo1.png');. I have error
A value of type 'Image' can't be assigned to a variable of type 'File'.
Try changing the type of the variable, or casting the right-hand type to 'File'.
So, my question is how I can send my image using MultipartFile method?
First, obtain the asset as list of bytes:
var bytes = (await rootBundle.load('images/photo1.png')).buffer.asUint8List();
Then use that in the MultipartFile named constructor:
var mpFile = http.MultipartFile.fromBytes('img', bytes, filename: 'photo.jpg');
My question is if there is a way to load a .pdf file in app resources like assets when deploying flutter app ? I'd like to display a PDF file using this:
https://pub.dartlang.org/packages/flutter_full_pdf_viewer
but without loading from internet ;)
Copy the asset to a temporary file.
Future<File> copyAsset() async {
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
File tempFile = File('$tempPath/copy.pdf');
ByteData bd = await rootBundle.load('assets/asset.pdf');
await tempFile.writeAsBytes(bd.buffer.asUint8List(), flush: true);
return tempFile;
}