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)
Related
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);
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.
Hi I am developing a flutter application. In this application some sounds are downloaded from my website and saved to the local file storage. It plays when some pictures are showed.
This works in Android phone properly. But I cannot do it in IOS Simulator. I have controlled whether the files are downloaded properly. I have looked simulator files and saw sound files, so they are properly downloaded. My playing sound code is like that.
Future play(File soundFile) async {
debugPrint("sound: play enter");
try {
await advancedPlayer.stop();
advancedPlayer.setReleaseMode(ReleaseMode.RELEASE);
if (soundFile != null) {
print(soundFile.uri);
advancedPlayer.play(soundFile.uri.toString(), isLocal: true);
await advancedPlayer.onPlayerCompletion.first;
debugPrint("sound: played");
// advancedPlayer.stop();
}
} catch (e) {
debugPrint("sound: error");
}
debugPrint("sound: play leave");
}
In this code the program waits in this line and not doesn't go to the next lines.
await advancedPlayer.onPlayerCompletion.first;
I don't see any errors.
I have printed sound file path and controlled it, it is in the file storage. Format of audio file is mp3.
In my pubspec.yaml file I have tried 0.17.4 and 0.16.2 versions. I cannot use 0.18.x versions(because other packages dont let me)
audioplayers: ^0.17.4
Thank you so much
[✓] Flutter (Channel stable, 2.0.4, on Mac OS X 10.15.6 19G2021 darwin-x64, locale en-TR)
• Flutter version 2.0.4 at /Users/varyok/flutter
• Framework revision b1395592de (2 weeks ago), 2021-04-01 14:25:01 -0700
• Engine revision 2dce47073a
• Dart version 2.12.2
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
• Android SDK at /Users/varyok/Library/Android/sdk
• Platform android-30, build-tools 30.0.2
• 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)
• All Android licenses accepted.
[!] Xcode - develop for iOS and macOS
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 12.0, Build version 12A7209
! Xcode 12.0.0 out of date (12.0.1 is recommended).
Download the latest version or update via the Mac App Store.
• CocoaPods version 1.10.0
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] 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)
[✓] VS Code (version 1.55.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.21.0
[✓] Connected device (2 available)
• iPhone 11 (mobile) • EC15BA0B-9EA7-4FCB-9DD0-E7A6F8B30B95 • ios •
com.apple.CoreSimulator.SimRuntime.iOS-14-0 (simulator)
• Chrome (web) • chrome • web-javascript • Google
Chrome 89.0.4389.128
shared_preferences: ^0.5.12+4
Error log:
MissingException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences)
flutter sdk details:
[✓] Flutter (Channel stable, 1.22.5, on Mac OS X 10.14.6 18G7016 darwin-x64, locale en-US)
• Flutter version 1.22.5 at /Users/taleb/Developer/flutter
• Framework revision 7891006299 (6 weeks ago), 2020-12-10 11:54:40 -0800
• Engine revision ae90085a84
• Dart version 2.10.4
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at /Users/taleb/Library/Android/sdk
• Platform android-30, build-tools 30.0.3
• ANDROID_HOME = /Users/taleb/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-6915495)
• All Android licenses accepted.
[!] Xcode - develop for iOS and macOS
✗ Xcode installation is incomplete; a full installation is necessary for iOS development.
Download at: https://developer.apple.com/xcode/download/
Or install Xcode via the App Store.
Once installed, run:
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
sudo xcodebuild -runFirstLaunch
• CocoaPods version 1.8.3
[✓] Android Studio (version 4.1)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin installed
• Dart plugin version 201.9317
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
[✓] IntelliJ IDEA Ultimate Edition (version 2019.3)
• IntelliJ at /Applications/IntelliJ IDEA.app
• Flutter plugin version 45.1.2
• Dart plugin version 193.7547
[✓] Connected device (1 available)
• SM A107F (mobile) • R9AM905ZREJ • android-arm • Android 10 (API 29)
! Doctor found issues in 1 category.
If you're using shared_preferences 0.2.4 and above, use setMockInitialValues:
SharedPreferences.setMockInitialValues({}); // set initial values here if desired
For earlier versions you can do it manually:
const MethodChannel('plugins.flutter.io/shared_preferences')
.setMockMethodCallHandler((MethodCall methodCall) async {
if (methodCall.method == 'getAll') {
return <String, dynamic>{}; // set initial values here if desired
}
return 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;
});
}