cannot create a local file using await getApplicationDocumentsDirectory(); - flutter

when I run the following function
_makeFile() async{
final dataFile = 'data.txt';
final directory = await getApplicationDocumentsDirectory();
final file = File('${directory.path}/$dataFile');
var isExist = await file.exists();
if (isExist) {
print('File exists');
} else {
print('file does not exist');
}
}
I get :
flutter: file does not exist
Flutter doctor :
jfb # ezer in ~/FTest/file_exist [12:16:41]
$ flutter doctor -v
[✓] Flutter (Channel stable, v1.12.13+hotfix.5, on Mac OS X 10.15.2 19C57, locale en-FR)
• Flutter version 1.12.13+hotfix.5 at /usr/local/flutter
• Framework revision 27321ebbad (4 weeks ago), 2019-12-10 18:15:01 -0800
• Engine revision 2994f7e1e6
• Dart version 2.7.0
[!] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
• Android SDK at /Users/jfb/Library/Android/sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-28, build-tools 28.0.3
• Java binary at: /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home/bin/java
• Java version Java(TM) SE Runtime Environment 18.3 (build 10.0.2+13)
! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses
[✓] Xcode - develop for iOS and macOS (Xcode 11.3)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 11.3, Build version 11C29
• CocoaPods version 1.8.3
[!] Android Studio (not installed)
• Android Studio not found; download from https://developer.android.com/studio/index.html
(or visit https://flutter.dev/setup/#android-setup for detailed instructions).
[✓] Connected device (1 available)
• iPhone 11 Pro Max • 1669648E-FBC3-41D7-9DA0-8E4FF5EBB360 • ios •
com.apple.CoreSimulator.SimRuntime.iOS-13-3 (simulator)
! Doctor found issues in 2 categories.
I just do not understand what's happening, I used this approach in order to store json without problem
Thanks for any pointer ...
JF

This line :
final file = File('${directory.path}/$dataFile');
does not create a File but only a reference.
To create a File, you need to use writeAsString() or writeAsBytes().
Retry by adding :
final dataFile = 'data.txt';
final directory = await getApplicationDocumentsDirectory();
final file = File('${directory.path}/$dataFile');
await file.writeAsString('This is data.txt content'); // <-- This line
print(await file.exists() ? 'Exists' : 'Do not exists');

Related

Flutter writeAsBytes from dart:io library returns PathNotFoundException

I am trying to use the following function in flutter:
Future<File> getFile(String fileName) async {
final appDir = await getTemporaryDirectory();
final appPath = appDir.path;
final fileOnDevice = File('$appPath/$fileName');
final rawAssetFile = await rootBundle.load(fileName);
final rawBytes = rawAssetFile.buffer.asUint8List();
await fileOnDevice.writeAsBytes(rawBytes, flush: true);
return fileOnDevice;
}
But somehow i keep getting this error, failed PathNotFoundException: Cannot open file, path = '/var/mobile/Containers/Data/Application/352527E9-XXXX-XXXX-XXXX-DCFXXXXXXXXX/Library/Caches/assets/
The function is called by final dataFile = await getFile('assets/sample.txt');
Does anyone know how to solve this? Any help is appreciated!
This is my flutter doctor -v
[✓] Flutter (Channel stable, 3.7.0, on macOS 13.1 22C65 darwin-arm64, locale en-SG)
• Flutter version 3.7.0 on channel stable at /Users/xaviertoh/Documents/Flutter/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision b06b8b2710 (3 weeks ago), 2023-01-23 16:55:55 -0800
• Engine revision b24591ed32
• Dart version 2.19.0
• DevTools version 2.20.1
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
• Android SDK at /Users/xaviertoh/Library/Android/sdk
• Platform android-33, build-tools 33.0.1
• Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 14C18
• CocoaPods version 1.11.3
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2021.3)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
[✓] VS Code (version 1.75.0)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.58.0
[✓] Connected device (3 available)
• iPhone (mobile) • 00008020-000D05313C02002E • ios • iOS 13.5 17F75
• macOS (desktop) • macos • darwin-arm64 • macOS 13.1 22C65
darwin-arm64
• Chrome (web) • chrome • web-javascript • Google Chrome 110.0.5481.77
[✓] HTTP Host Availability
• All required HTTP hosts are available
• No issues found!
Thank you for reading!
Things i have tried:
I have confirmed that my file name is correct in the assets folder and i also tried running this code in various locations in the code such as in a widget and also in main() just to test it out.
I also tried running flutter in xcode and visual studio code.
Also tried to run the code in android emulator and also on iPhone physical device.
The /var/mobile/Containers/Data/Application/.../Library/Caches/assets/ directory must already exist. File.writeAsBytes won't automatically create parent directories for you.
You can do that step by first doing:
await fileOnDevice.parent.create(recursive: true);

Generating image files from CameraImage

We would like to use CameraController.startImageStream() instead of CameraController.takePicture() to get a return value equivalent to CameraController.takePicture(). We know that various answers have been documented for these but we have not yet succeeded with any of the methods.
We have implemented the following. We tried setting the imageFormatGroup during CameraController generation, but this did not improve things. We have to make these work on both Android and iOS. We want a way to run it faster. It can be written in Flutter or using C++ and ffi.
import 'package:image/image.dart' as imglib;
final cameras = await availableCameras();
final firstCamera = cameras.first;
cameraController = CameraController(
firstCamera,
ResolutionPreset.medium,
// imageFormatGroup: ImageFormatGroup.yuv420,
);
await cameraController!.initialize();
await cameraController!.startImageStream((CameraImage cameraImage) async {
cameraController!.stopImageStream();
Uint8List list = cameraImage.planes.first.bytes;
final width = cameraImage.width;
final height = cameraImage.height;
imglib.Image test = imglib.Image.fromBytes(width, height, list);
List<int> _imageBytes = imglib.encodeJpg(test);
Uint8List convert = Uint8List.fromList(_imageBytes);
File file = File.fromRawPath(convert);
XFile? result = XFile(file.path);
});
Index out of range occurred when we called encodeJpg(). How should we fix this?
fvm flutter doctor -v
[✓] Flutter (Channel stable, 3.3.6, on macOS 12.6 21G115 darwin-x64, locale en-JP)
• Flutter version 3.3.6 on channel stable at /Users/xxxxx/fvm/versions/stable
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 6928314d50 (3 weeks ago), 2022-10-25 16:34:41 -0400
• Engine revision 3ad69d7be3
• Dart version 2.18.2
• DevTools version 2.15.0
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
• Android SDK at /Users/xxxxx/src/android-sdks
• Platform android-33, build-tools 33.0.0
• Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 13F100
• CocoaPods version 1.11.2
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2021.2)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
[✓] VS Code (version 1.73.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.52.0
[✓] Connected device (3 available)
• SHV43 (mobile) • XXXXXXXXXXXXXXX • android-arm64 • Android 10 (API 29)
• macOS (desktop) • macos • darwin-x64 • macOS 12.6 21G115 darwin-x64
• Chrome (web) • chrome • web-javascript • Google Chrome 107.0.5304.110
[✓] HTTP Host Availability
• All required HTTP hosts are available
• No issues found!
I assume your problem comes from mixing different image formats.
The camera stream in this line
Uint8List list = cameraImage.planes.first.bytes
returns an image that is encoded with YUV instead of the common RGB. So before further using this image, I would recommend converting it.
There are multiple sources already out there on how to do that conversion. Let me point you to this blog post and the following stackoverflow questions: 1 2.
You certainly can implement this conversion via ffi, but it is not strictly necessary.

Flutter - image picker issue at release apk

i am a newbe at programming, i am very interested in flutter(dart).
Now i am facing my first big problem and i hope someone can help me out.
I would like use the flutter image picker pakage to get a image from my gallary or camera and show my users on the screen. So far so good and it works on emulator, also on my device but just if its connected to my pc, but if i build a release apk and send it to any device it doesent work. Watch at the videos.
Here it works:
https://streamable.com/3nj93j
Here the release apk Version
https://streamable.com/ba7eil
What i tried:
I have found few similar problams and i thounght it is a storage problem, i used:
android:requestLegacyExternalStorage="true"
but it doesent help, also i tried set the sdk from 30 to 28, nothing helps
Update: I build the app-debug.apk and send it to my device, it works fine.
I dont get it why the app-relase.apk ist not working???
Here my Methode:
Future getImage() async {
PickedFile pickedFile = await picker.getImage(source: ImageSource.camera);
setState(() {
if (pickedFile != null) {
_image = File(pickedFile.path);
} else {
print('No image selected.');
}
});
}
I hope anyone have a idea
[√] Flutter (Channel stable, 1.22.6, on Microsoft Windows [Version 10.0.18363.1316], locale de-AT)
• Flutter version 1.22.6 at C:\Flutter\flutter
• Framework revision 9b2d32b605 (3 weeks ago), 2021-01-22 14:36:39 -0800
• Engine revision 2f0af37152
• Dart version 2.10.5
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at C:/Users/acas1/AppData/Local/Android/Sdk
• Platform android-30, build-tools 30.0.3
• ANDROID_HOME = C:/Users/acas1/AppData/Local/Android/Sdk
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses
[√] Android Studio (version 4.0)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin installed
• Dart plugin version 193.7547
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
[√] Connected device (1 available)
• SM F916B (mobile) • R3CN80VJP2V • android-arm64 • Android 11 (API 30)

Cannot open/read local files in test. Throws FileSystemException: Cannot open file, path

Hi I am trying to read a json file in my test class.
test(
'should return a valid model when the JSON is populated with images',
() async {
// Given
File file = new File('resources/image_fixture.json');
String jsonString = await file.readAsString();
// When
final result = Image.fromJson(jsonDecode(jsonString));
// Then
expect(result, subject);
},
);
But when I read the file, I get the following error
FileSystemException: Cannot open file, path = 'resources/image_fixture.json' (OS Error: No such file or directory, errno = 2)
This is my file structure
Flutter doctor:
[✓] Flutter (Channel stable, 1.22.1, on Mac OS X 10.15.6 19G2021, locale en-CA)
• Flutter version 1.22.1 at /Users/asharma/Library/flutter
• Framework revision f30b7f4db9 (13 days ago), 2020-10-08 10:06:30 -0700
• Engine revision 75bef9f6c8
• Dart version 2.10.1
[!] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
• Android SDK at /Users/asharma/Library/Android/sdk
• Platform android-30, build-tools 29.0.3
• ANDROID_HOME = /Users/asharma/Library/Android/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses
[✓] Xcode - develop for iOS and macOS (Xcode 11.6)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 11.6, Build version 11E708
• CocoaPods version 1.9.3
[✓] Android Studio (version 4.0)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 49.0.2
• Dart plugin version 193.7547
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
[!] IntelliJ IDEA Community Edition (version 2019.3.4)
• IntelliJ at /Applications/IntelliJ IDEA CE.app
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.
• For information about installing plugins, see
https://flutter.dev/intellij-setup/#installing-the-plugins
[✓] VS Code (version 1.48.0)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.12.2
Can someone please help me find what is going on?
get it outside the test directory and:
String file = await rootBundle.loadString("/path/to")
You can try flutter test test instead of flutter test command from the root directory. Source:
https://github.com/flutter/flutter/issues/12999
https://github.com/flutter/flutter/issues/20907

ImagePicker.pickImage(source: ImageSource.gallery) returns null

Steps to Reproduce
Create a flutter app. Try and use the most basic functionality of the image_picker plugin.
Expected results:
When you select an image from the gallery, it should return something to your application. A file, or location. Anything really.
Actual results:
Nothing. The gallery launches. You select an image. Gallery goes away, nothing is returned to the app at all. Just leaves null.
My code
Future getImage() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
customImageFile = image.toString();
print('customImageFile: ' + customImageFile);
}
Output from code
customImageFile: null
Plugin Version
image_picker: 0.6.5
Flutter doctor
[√] Flutter (Channel stable, v1.12.13+hotfix.5, on Microsoft Windows [Version 10.0.17763.1098], locale en-AU)
• Flutter version 1.12.13+hotfix.5 at c:\flutter
• Framework revision 27321eb (4 months ago), 2019-12-10 18:15:01 -0800
• Engine revision 2994f7e1e6
• Dart version 2.7.0
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
• Android SDK at C:\Installations\AndroidSDK
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-29, build-tools 29.0.2
• ANDROID_HOME = C:\Installations\AndroidSDK
• Java binary at: C:\Installations\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
• All Android licenses accepted.
[√] Android Studio (version 3.5)
• Android Studio at C:\Installations\Android Studio
• Flutter plugin version 40.2.2
• Dart plugin version 191.8593
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
[√] Connected device (1 available)
• SM G970F • android-arm64 • Android 10 (API 29)
• No issues found!
Future getImage() async {
var image = await ImagePicker().getImage(source: ImageSource.gallery);
customImageFile = File(image.path);
print('customImageFile: ' + customImageFile);
}
Flutter 2
You should typecast image variable as File:
var image = await ImagePicker().getImage(source: ImageSource.gallery) as File;
The image_picker returns null on IOS simulator on M1 Macs. This is a problem that is still not resolved. Use a physical device. Your code seems correct.
flutter
you need to create future function.
store the result in a variable of File bcz it will return
the image Path
Future<File> pickImage(ImageSource source) async{
File testImage = await ImagePicker.pickImage(source: source);
setState(() {
pickedImage = testImage;
});
}