I found app crashes on Android when I pick a file named too long.
In getUriFromRemote() in FileUtils.java, when a file has too long name, FileOutputStream throws FileNotFoundException and fos.close() in the catch statement can throw NullPointerException but it is not catched anywhere so app will crash.
try {
fos = new FileOutputStream(externalFile); // throws FileNotFoundException
try {
BufferedOutputStream out = new BufferedOutputStream(fos);
InputStream in = context.getContentResolver().openInputStream(uri);
byte[] buffer = new byte[8192];
int len = 0;
while ((len = in.read(buffer)) >= 0) {
out.write(buffer, 0, len);
}
out.flush();
} finally {
fos.getFD().sync();
}
} catch (Exception e) {
try {
// fos is null so NullPointerException is thrown and app crashes
fos.close();
} catch(IOException ex) {
Log.e(TAG, "Failed to close file streams: " + e.getMessage(),null);
return null;
}
Log.e(TAG, "Failed to retrieve path: " + e.getMessage(),null);
return null;
}
Thanks.
[ ] E/FilePickerUtils(25796): Getting for API 19 or abovecontent://com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3Dencoded%3Dt0pb4SexMA1yevTPlz%2B7NoqLkGJn2bU3DEqYZqCdKxC94%2FjnWXLf3us7aIyx
[ +11 ms] E/FilePickerUtils(25796): Document URI
[ ] I/FilePickerUtils(25796): Caching file from remote/external URI
[ +21 ms] D/AndroidRuntime(25796): Shutting down VM
[ ] E/AndroidRuntime(25796): FATAL EXCEPTION: main
[ ] E/AndroidRuntime(25796): Process: jp.co.cyberagent.detroit.dev04, PID: 25796
[ ] E/AndroidRuntime(25796): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=48496, result=-1, data=Intent { dat=content://com.google.android.apps.docs.storage/document/acc=1;doc=encoded=t0pb4SexMA1yevTPlz+7NoqLkGJn2bU3DEqYZqCdKxC94/jnWXLf3us7aIyx flg=0x1 }} to activity {jp.co.cyberagent.detroit.dev04/detroit.requapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.FileOutputStream.close()' on a null object reference
[ ] E/AndroidRuntime(25796): at android.app.ActivityThread.deliverResults(ActivityThread.java:4372)
[ +1 ms] E/AndroidRuntime(25796): at android.app.ActivityThread.handleSendResult(ActivityThread.java:4416)
[ ] E/AndroidRuntime(25796): at android.app.ActivityThread.-wrap19(Unknown Source:0)
[ ] E/AndroidRuntime(25796): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1655)
[ ] E/AndroidRuntime(25796): at android.os.Handler.dispatchMessage(Handler.java:105)
[ ] E/AndroidRuntime(25796): at android.os.Looper.loop(Looper.java:251)
[ ] E/AndroidRuntime(25796): at android.app.ActivityThread.main(ActivityThread.java:6599)
[ ] E/AndroidRuntime(25796): at java.lang.reflect.Method.invoke(Native Method)
[ ] E/AndroidRuntime(25796): at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
[ ] E/AndroidRuntime(25796): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
[ ] E/AndroidRuntime(25796): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.FileOutputStream.close()' on a null object reference
[ ] E/AndroidRuntime(25796): at com.mr.flutter.plugin.filepicker.FileUtils.getUriFromRemote(FileUtils.java:201)
[ ] E/AndroidRuntime(25796): at com.mr.flutter.plugin.filepicker.FilePickerPlugin$1.onActivityResult(FilePickerPlugin.java:84)
[ +1 ms] E/AndroidRuntime(25796): at io.flutter.app.FlutterPluginRegistry.onActivityResult(FlutterPluginRegistry.java:218)
[ +1 ms] E/AndroidRuntime(25796): at io.flutter.app.FlutterActivityDelegate.onActivityResult(FlutterActivityDelegate.java:143)
[ ] E/AndroidRuntime(25796): at io.flutter.app.FlutterActivity.onActivityResult(FlutterActivity.java:142)
[ ] E/AndroidRuntime(25796): at android.app.Activity.dispatchActivityResult(Activity.java:7235)
[ ] E/AndroidRuntime(25796): at android.app.ActivityThread.deliverResults(ActivityThread.java:4368)
[ ] E/AndroidRuntime(25796): ... 9 more
[β] Flutter (Channel unknown, v1.3.8, on Mac OS X 10.13.6 17G65, locale ja-JP)
[β] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[β] iOS toolchain - develop for iOS devices (Xcode 10.1)
[β] Android Studio (version 3.4)
[β] IntelliJ IDEA Community Edition (version 2018.1.2)
[β] VS Code (version 1.35.1)
[β] Connected device (3 available)
Good catch @kitoko552! However, itβs weird that it wonβt load because of a big name.
How can you replicate it?
Thank you.
Thank you for your reply!
My app is for Japanese so the crash is occurred by picking a Japanese named file.
It is replicated when you pick a file named about 200 Japanese characters from Google Drive.
Something like this.
γγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγ.pdf
I know it is not usual naming actually but it is possible so I think it is better to be fixed.
Thank you.
@kitoko552 thank you. I'll see what I can do about it.
@kitoko552 somehow, I can't replicate the issue. I've given to a file the exact same name and tried to load it using the example app and it works just fine:
I/FilePickerUtils(11980): Caching file from remote/external URI
I/FilePickerUtils(11980): File loaded and cached at:/data/user/0/com.mr.flutter.plugin.filepickerexample/cache/γγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγ.pdf
I/FilePicker(11980): Absolute file path:/data/user/0/com.mr.flutter.plugin.filepickerexample/cache/γγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγγ.pdf
Can you give me more details about how to exactly replicate it?
Thank you.
How about this?
ζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγζ₯ζ¬θͺγγγγγ.pdf
The issue is replicated in my Nexus 5x(Android 8.1.0) and Xperia XZ2(Android 8.0.0).
@kitoko552 Thank you, I'll try to emulate the Nexus 5x with Android 8 and see if I can replicate it. I'll keep you updated.
I was able to reproduce and fix the issue with that file name @kitoko552. The exception that was being thrown is indeed related to the file name being too long.
Anyway, this is now fixed in 1.3.8.
Thank you for reporting.
Thank you for fixing!