How to convert file back to Asset

470 views Asked by At

I am using the multiple file picker package on pub.dev, the link is below https://pub.dev/packages/multi_image_picker in conjunction with the image cropper package by Yalantis https://pub.dev/packages/image_cropper to let my user pick multiple images and then crop them at will.

I am using this code to convert my asset into a file and feed it into the cropper. And it works.

final temp = await Directory.systemTemp.createTemp();
final data = await finalList[index].getByteData();
File failo = await File('${temp.path}/img').writeAsBytes(
    data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));

print("The path is ${temp.path}");
File croppedFailo = await ImageCropper.cropImage(
  sourcePath: failo.path,
  androidUiSettings: AndroidUiSettings(toolbarTitle: "My App"),
);

The tricky bit is to convert it back to an asset so that i can replace the old uncropped asset with this new cropped one..I read through the Asset documentation of the package and I tried this but it made my app crash

 Asset croppedPic = new Asset(
  croppedFailo.path,
  DateTime.now().millisecondsSinceEpoch.toString(),
  300,
  300,
);

finalList.replaceRange(index, index + 1, [croppedPic]);

EDIT: When i say "asset", i am not referring to images i manually added to the assets/images folder in the app. The multi image picker plugin has a file- type called asset in which it returns images. That is the type to which i want to convert my file back into.

1

There are 1 answers

0
Simeon On

Never mind. I figured it's too unnecessarily complicated to do that. So, i instead just reformatted my entire code to handle images as files instead of assets. And, it actually made my life a lot simpler coz files give you more versatility and less problems than assets.