Cannot open file, path = '/data/data/com.example.demo/cache/a.png' (OS Error: No such file or directory, errno = 2))
I want to do my generated Image convert to XFile. when i am trying then i face this issue (Cannot open file, path = '/data/data/com.example.demo/cache/a.png' (OS Error: No such file or directory, errno = 2)).
code inside function:
final XFile? pickedImage = await ImagePicker().pickImage(source: source);
if (pickedImage != null) {
//for convert greyscale
final Uint8List imgBytes = await File(pickedImage.path).readAsBytes();
final imgLib.Image? image = imgLib.decodeImage(imgBytes);
img = imgLib.grayscale(image!);
print("object img: ${img!.getBytes()}");
final root = await getTemporaryDirectory();
final path = "${root.path}/a.png";
print("object path: $path");
imageFile = XFile(path,bytes: img!.getBytes());
and ui code:
image: FileImage(File(imageFile!.path)),
CodePudding user response:
you are facing this error because App is not going to search all over the file manager to get this image on different devices. so store the base64 string in a variable and you can decode that also. btw your question I tried best to answer in all ways I saw your code I think you are also converting it into bytes and base64. you can use the code below. it will pick the image from the gallery after that it will store it as Xfile and then read the bytes of it and convert the bytes into base 64 string
//Variables
ImagePicker picker = ImagePicker();
XFile? image;
File? f = null;
//picking image from gallery
InkWell(
onTap: () async {
image = await picker.pickImage(source: ImageSource.gallery);
setState(() {
f = File(image!.path);});
},
child: Center(
child: f == null?
Image.asset('assets/images/placeholder.png',
width: 194, height: 174)
: ClipRRect( borderRadius:BorderRadius.all(
Radius.circular(10),),
child: Image.file(f!, width: 194,height:174,
fit: BoxFit.cover,)),
),
),
//converting now to xfile => byte => base64
if (f != null) {
const FlutterSecureStorage storage =
FlutterSecureStorage();
setState(() {
APIcalling = true;
});
final bytes = f!.readAsBytesSync();
List<int> imageBytes =
await image!.readAsBytes();
final String b64 =
base64Encode(imageBytes);
log(b64);