I and my friends want to make a media player app in Android TV using Flutter for showing audio, video, and image.
When we want to show picture preview using XFile from cross_file package and FlutterImageCompress from flutter_image_compress package by resizing the image, some image files in TV storage are working and no problem when we focused to a image in all image list.
But when we focused to a image file to show image preview, suddenly the app crashed and show error :
java.lang.Error: java.io.FileNotFoundException: /storage/emulated/0/Pictures/83750669-closeup-of-binturong-or-arctictis-binturong-in-zoo.jpg: open failed: EFAULT (Bad address)
Full stack trace:
I/flutter (12717): imageFile.path : /storage/emulated/0/Pictures/83750669-closeup-of-binturong-or-arctictis-binturong-in-zoo.jpg
E/AndroidRuntime(12717): FATAL EXCEPTION: pool-3-thread-5
E/AndroidRuntime(12717): Process: com.example.mediaplayer, PID: 12717
E/AndroidRuntime(12717): java.lang.Error: java.io.FileNotFoundException: /storage/emulated/0/Pictures/83750669-closeup-of-binturong-or-arctictis-binturong-in-zoo.jpg: open failed: EFAULT (Bad address)
E/AndroidRuntime(12717): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1173)
E/AndroidRuntime(12717): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
E/AndroidRuntime(12717): at java.lang.Thread.run(Thread.java:923)
E/AndroidRuntime(12717): Caused by: java.io.FileNotFoundException: /storage/emulated/0/Pictures/83750669-closeup-of-binturong-or-arctictis-binturong-in-zoo.jpg: open failed: EFAULT (Bad address)
E/AndroidRuntime(12717): at libcore.io.IoBridge.open(IoBridge.java:492)
E/AndroidRuntime(12717): at java.io.FileInputStream.<init>(FileInputStream.java:160)
E/AndroidRuntime(12717): at kotlin.io.FilesKt__FileReadWriteKt.readBytes(FileReadWrite.kt:63)
E/AndroidRuntime(12717): at com.fluttercandies.flutter_image_compress.core.CompressFileHandler.handle$lambda-0(CompressFileHandler.kt:36)
E/AndroidRuntime(12717): at com.fluttercandies.flutter_image_compress.core.CompressFileHandler.$r8$lambda$YnMDWvgbTcy5MzKb5z0NWIZwJ_o(Unknown Source:0)
E/AndroidRuntime(12717): at com.fluttercandies.flutter_image_compress.core.CompressFileHandler$$ExternalSyntheticLambda0.run(Unknown Source:4)
E/AndroidRuntime(12717): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
E/AndroidRuntime(12717): ... 2 more
E/AndroidRuntime(12717): Caused by: android.system.ErrnoException: open failed: EFAULT (Bad address)
E/AndroidRuntime(12717): at libcore.io.Linux.open(Native Method)
E/AndroidRuntime(12717): at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
E/AndroidRuntime(12717): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
E/AndroidRuntime(12717): at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
E/AndroidRuntime(12717): at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7542)
E/AndroidRuntime(12717): at libcore.io.IoBridge.open(IoBridge.java:478)
E/AndroidRuntime(12717): ... 8 more
I/Process (12717): Sending signal. PID: 12717 SIG: 9
But strangely, when we focused to the another file, there's no problem to show the image preview.
I've tried to using try-catch for this solution so it returns null because of error open the data, but it didn't work and still crashed when we tried to focus on a image which have a problem on it.
This is the code :
StreamBuilder<XFile?>(
stream: _resize(), // From here
builder: (context, snapshot) {
if (snapshot.hasData &&
snapshot.connectionState == ConnectionState.done) {
// using image path if done and error builder for if the image failed to show
} else {
// using loading if not done yet
}
}
),
Stream<XFile?> _resize() async* {
_selectedImage = await _resizeImage(XFile(widget.picFile.path)); // Then enter _resizeImage function
yield _selectedImage;
}
Future<XFile?> _resizeImage(XFile imageFile) async {
try {
debugPrint("imageFile.path : ${imageFile.path}");
List<int> imageBytes = (await FlutterImageCompress.compressWithFile(imageFile.path, quality: 25)) as List<int>; // This is the error line, try-catch is not working and the app crashed :(
String fName = imageFile.name;
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
String compressedImagePath = '$appDocPath/$fName.jpg';
await File(compressedImagePath).writeAsBytes(imageBytes);
return XFile(compressedImagePath);
}
catch (e) {
return null;
}
}
So, how can I solve this problem? There is no problem with file permission and the file path is correct.