miguelpruivo / flutter_file_picker

File picker plugin for Flutter, compatible with mobile (iOS & Android), Web, Desktop (Mac, Linux, Windows) platforms with Flutter Go support.
MIT License
1.36k stars 676 forks source link

App crash on file pick in android #286

Closed maxbhoria closed 4 years ago

maxbhoria commented 4 years ago

Hi i am not able to pick file app get crashed when i select any file.

I an using android device.

Issue printed in console

E/AndroidRuntime( 1459): java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference E/AndroidRuntime( 1459): at com.mr.flutter.plugin.filepicker.FilePickerDelegate$2.run(FilePickerDelegate.java:92) E/AndroidRuntime( 1459): at java.lang.Thread.run(Thread.java:919)

miguelpruivo commented 4 years ago

Hi, how are you picking the file?

maxbhoria commented 4 years ago

Hi, how are you picking the file?

I am using this code

void pickFile() async { setState(() => _loadingPath = true); try { _path = null; _path = await FilePicker.getFilePath( type: _pickingType, allowedExtensions: (_extension?.isNotEmpty ?? false) ? _extension?.replaceAll(' ', '')?.split(',') : null); } on PlatformException catch (e) { print("Unsupported operation" + e.toString()); } if (!mounted) return; setState(() { _loadingPath = false; _fileName = _path != null ? _path.split('/').last : _paths != null ? _paths.keys.toString() : '...'; }); }

miguelpruivo commented 4 years ago

Please, don’t use that. That’s an example code that’s made to fit a lot of use cases with a few lines and shouldn’t be used at on your project.

Check the plugin’s Wiki that has a lot of well documented examples for each use case and let me know if it worked for you.

Thanks!

maxbhoria commented 4 years ago

Please, don’t use that. That’s an example code that’s made to fit a lot of use cases with a few lines and shouldn’t be used at on your project.

Check the plugin’s Wiki that has a lot of well documented examples for each use case and let me know if it worked for you.

Thanks!

Now i used this code but app still crashing

File extraFile = await FilePicker.getFile(type: FileType.any); setState(() { fileName = basename(extraFile.path); });

Error Logs E/AndroidRuntime(20193): java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference E/AndroidRuntime(20193): at com.mr.flutter.plugin.filepicker.FilePickerDelegate$2.run(FilePickerDelegate.java:92) E/AndroidRuntime(20193): at java.lang.Thread.run(Thread.java:919) E/FilePickerUtils(20193): Document URI E/FilePickerUtils(20193): External Document URI E/FilePickerUtils(20193): Primary External Document URI I/FilePickerDelegate(20193): Absolute file path:/storage/emulated/0/Download/dbs-postgraduate-fees-2015-16.pdf

miguelpruivo commented 4 years ago

@maxbhoria when you run the example app, does that happen?

maxbhoria commented 4 years ago

@maxbhoria when you run the example app, does that happen? No this is not happened when i download code from github.

miguelpruivo commented 4 years ago

Hum, there must be something in your project that's resulting in type being null. Which file_picker version are you on? By looking at your stack trace, it's invoking type as null in a place where it can't be null.

Also, run a full flutter clean and build again.

maxbhoria commented 4 years ago

Hum, there must be something in your project that's resulting in type being null. Which file_picker version are you on? By looking at your stack trace, it's invoking type as null in a place where it can't be null.

Also, run a full flutter clean and build again.

Plugin version is file_picker: ^1.10.0 tried many time with flutter clean

Can you tell me how to directly use github plugin directly in my app?

miguelpruivo commented 4 years ago

If you can't reproduce it with the example app, then likely there must be something related to some conflict on your project.

There's no need to use the github project directly on your project as it won't make any difference or whatsoever, but may help you get rid of cached issues. You can give it a try. Just use it like this:

file_picker:
  git: https://github.com/miguelpruivo/flutter_file_picker.git
maxbhoria commented 4 years ago

If you can't reproduce it with the example app, then likely there must be something related to some conflict on your project.

There's no need to use the github project directly on your project as it won't make any difference or whatsoever, but may help you get rid of cached issues. You can give it a try. Just use it like this:

file_picker:
  git: https://github.com/miguelpruivo/flutter_file_picker.git

ok thanks, let me try this!

maxbhoria commented 4 years ago

file_picker: git: https://github.com/miguelpruivo/flutter_file_picker.git

it also did not worked error is same

miguelpruivo commented 4 years ago

Yes, like I told you, pubdev has the same version. I really would like to help you, but I quite don't get what might be triggering it. Maybe post your code block that's asking for the picker.

maxbhoria commented 4 years ago

Yes, like I told you, pubdev has the same version. I really would like to help you, but I quite don't get what might be triggering it. Maybe post your code block that's asking for the picker.

however it does not require any specific setting for android

maxbhoria commented 4 years ago

Yes, like I told you, pubdev has the same version. I really would like to help you, but I quite don't get what might be triggering it. Maybe post your code block that's asking for the picker.

This is complete log have a look in that please

D/SurfaceView(31692): windowStopped(true) false io.flutter.embedding.android.FlutterSurfaceView{9be883b V.E...... ........ 0,0-1080,2301} of ViewRootImpl@98d2c27[MainActivity] D/FlutterView(31692): Detaching from a FlutterEngine: io.flutter.embedding.engine.FlutterEngine@4d2e4be D/SurfaceView(31692): onWindowVisibilityChanged(4) false io.flutter.embedding.android.FlutterSurfaceView{9be883b V.E...... .......D 0,0-1080,2301} of ViewRootImpl@98d2c27[MainActivity] D/ViewRootImpl@98d2c27MainActivity: Relayout returned: old=(0,0,1080,2340) new=(0,0,1080,2340) req=(1080,2340)4 dur=12 res=0x1 s={false 0} ch=false I/FilePickerDelegate(31692): [SingleFilePick] File URI:content://com.android.externalstorage.documents/document/primary%3ADownload%2F4w2E4661.jpg E/FilePickerUtils(31692): Getting for API 19 or abovecontent://com.android.externalstorage.documents/document/primary%3ADownload%2F4w2E4661.jpg D/ViewRootImpl@98d2c27MainActivity: stopped(false) old=true D/SurfaceView(31692): windowStopped(false) false io.flutter.embedding.android.FlutterSurfaceView{9be883b V.E...... .......D 0,0-1080,2301} of ViewRootImpl@98d2c27[MainActivity] E/AndroidRuntime(31692): FATAL EXCEPTION: Thread-6 E/AndroidRuntime(31692): Process: com.babymotherprescription, PID: 31692 E/AndroidRuntime(31692): java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference E/AndroidRuntime(31692): at com.mr.flutter.plugin.filepicker.FilePickerDelegate$2.run(FilePickerDelegate.java:92) E/AndroidRuntime(31692): at java.lang.Thread.run(Thread.java:919) D/ViewRootImpl@98d2c27MainActivity: stopped(false) old=false E/FilePickerUtils(31692): Document URI E/FilePickerUtils(31692): External Document URI E/FilePickerUtils(31692): Primary External Document URI I/FilePickerDelegate(31692): Absolute file path:/storage/emulated/0/Download/4w2E4661.jpg D/SurfaceView(31692): onWindowVisibilityChanged(0) true io.flutter.embedding.android.FlutterSurfaceView{9be883b V.E...... .......D 0,0-1080,2301} of ViewRootImpl@98d2c27[MainActivity] D/ViewRootImpl@98d2c27MainActivity: Relayout returned: old=(0,0,1080,2340) new=(0,0,1080,2340) req=(1080,2340)0 dur=14 res=0x7 s={true 496188772352} ch=true D/OpenGLRenderer(31692): createReliableSurface : 0x740b61bd00, 0x738727d000 I/mali_winsys(31692): new_window_surface() [1080x2340] return: 0x3000 D/SurfaceView(31692): surfaceCreated 1 #8 io.flutter.embedding.android.FlutterSurfaceView{9be883b V.E...... .......D 0,0-1080,2301} D/SurfaceView(31692): surfaceChanged (1080,2301) 1 #8 io.flutter.embedding.android.FlutterSurfaceView{9be883b V.E...... .......D 0,0-1080,2301} I/Process (31692): Sending signal. PID: 31692 SIG: 9

maxbhoria commented 4 years ago

i tried with another new project but the output is same. Have you find anything or have you tried by creating new project to check?

maxbhoria commented 4 years ago

Do you remember any other settings that you did in your example because i don't how your example running perfectly

maxbhoria commented 4 years ago

is this the problem because of i am using kotlin and your plugin is in java?? Because i copy your android folder from the example and i paste it into my sample it worked for me

miguelpruivo commented 4 years ago

It shouldn't have any issue with kotlin projects. But try recreating your project from scratch or run flutter create . on your project so it can add missing files and run it again.

maxbhoria commented 4 years ago

Hello I commented this code in plugin FilePickerDeligate.java, will it create any further issue in my application?

// if (type.equals("dir") && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // uri = DocumentsContract.buildDocumentUriUsingTree(uri, DocumentsContract.getTreeDocumentId(uri)); // }

miguelpruivo commented 4 years ago

If you’re not planning to pick directories then not. I forgot to ask, what’s the minimum Android sdk of your app?

maxbhoria commented 4 years ago

If you’re not planning to pick directories then not. I forgot to ask, what’s the minimum Android sdk of your app?

yes of course i don't have plan to pick dir as of now but Please make a fix for this problem and my mini sdk is 21.

Thanks for your support

miguelpruivo commented 4 years ago

@maxbhoria I can’t make a fix if I don’t get what’s triggering your issue but I’ll give it a look, could you please tell me details of your app’s build gradle and which version your android device is on?

maxbhoria commented 4 years ago

You can check by creating a new project what wrong is triggered beacause i tried by creating new project also.

I am using Andorid 10 and this is app level gradle

def localProperties = new Properties() def localPropertiesFile = rootProject.file('local.properties') if (localPropertiesFile.exists()) { localPropertiesFile.withReader('UTF-8') { reader -> localProperties.load(reader) } }

def flutterRoot = localProperties.getProperty('flutter.sdk') if (flutterRoot == null) { throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.") }

def flutterVersionCode = localProperties.getProperty('flutter.versionCode') if (flutterVersionCode == null) { flutterVersionCode = '1' }

def flutterVersionName = localProperties.getProperty('flutter.versionName') if (flutterVersionName == null) { flutterVersionName = '1.0' }

apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android { compileSdkVersion 29

sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
}

lintOptions {
    disable 'InvalidPackage'
}

defaultConfig {
    // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
    applicationId "com.babymotherprescription"
    minSdkVersion 21
    targetSdkVersion 29
    versionCode flutterVersionCode.toInteger()
    versionName flutterVersionName
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        // TODO: Add your own signing config for the release build.
        // Signing with the debug keys for now, so `flutter run --release` works.
        signingConfig signingConfigs.debug
    }
}

}

flutter { source '../..' }

dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' }

nicolas-costa commented 4 years ago

@maxbhoria if you want a quick workaround, use file_picker: 1.7.0. I was facing the same issue here, strangely enough, the code was working on Friday 5. The older version "solved" it.

miguelpruivo commented 4 years ago

@nicolas-costa did you update it since Friday? The last update only introduced directory picks but for some reason this issue results in file type (apparently) being null where it can’t be, still trying to understand what might be resulting in it but no luck so far.

nicolas-costa commented 4 years ago

@miguelpruivo I didn't intentionally updated it. What I can tell you is that I installed it last week to make some tests and see if it would fit my needs. I followed the docs, where, as you know, it instructs to add "file_picker: ^1.7.1". It was working perfectly.

Then it stopped working somehow. I messed around trying to figure it out, without luck. Then stumbled on this version bump (1.10.0) on my pubspec file, found this issue and had the idea to rollback to 1.7. Boom, working again.

So I guess the package manager updated it for me due to the: ^.

miguelpruivo commented 4 years ago

@nicolas-costa using the carrot operator ^ should only update automatically patch versions (1.0.x) and not minor. Anyhow, is your log trace identically to this one with 1.10.0?

nicolas-costa commented 4 years ago

@miguelpruivo Yes, it is.

nicolas-costa commented 4 years ago

I searched about the carrot operator in dart dependency manager and found that it will update patch and minor versions.

https://dart.dev/tools/pub/dependencies#version-constraints

miguelpruivo commented 4 years ago

@maxbhoria just tried exactly like you, in a new project, Android 10 emulator, minSdk set to 21 and no issues whatsoever.

I'm running on a Mac but that shouldn't be a problem.

@nicolas-costa if you are experiencing the same issue with 1.10.0 I really would like to fix it, but I need to be able to replicate it, so please, give me all the details you can:

Thanks!

maxbhoria commented 4 years ago

@maxbhoria just tried exactly like you, in a new project, Android 10 emulator, minSdk set to 21 and no issues whatsoever.

I'm running on a Mac but that shouldn't be a problem.

@nicolas-costa if you are experiencing the same issue with 1.10.0 I really would like to fix it, but I need to be able to replicate it, so please, give me all the details you can:

  • Your build gradle version restrictions;
  • Your device version/name and if it's an emulator;
  • Other relevant info;

Thanks!

@miguelpruivo Thanks for your efforts i am also using mac machine with samsung M30s Real device with 10.

If you project is unable to get crash than i can do one thing i can share you my demo project code which i have tried as a sample. So that you address this issue.

Thanks

maxbhoria commented 4 years ago

@miguelpruivo I am sharing my sample app code in which i am getting the crash issue. Please check this out to address the bug. https://drive.google.com/file/d/1cS4kd_F9k_WByLZt9Zl0SPdkPCxuo6I_/view?usp=sharing

Thanks!

miguelpruivo commented 4 years ago

@maxbhoria thank you for your effort, just tried to replicate it with your project, and it's working perfectly. See the gif below: ezgif com-video-to-gif-2

maxbhoria commented 4 years ago

@maxbhoria thank you for your effort, just tried to replicate it with your project, and it's working perfectly. See the gif below: ezgif com-video-to-gif-2

have you checked it with real device?

miguelpruivo commented 4 years ago

@maxbhoria no, will try right now.

miguelpruivo commented 4 years ago

@maxbhoria update: just tried it, with a Samsung A40 on Android 10, works fine as well. 😞

maxbhoria commented 4 years ago

![file_picker](https://user-imag@miguelpruivo githubusercontent.com/32234760/84422905-54901c80-ac3b-11ea-9d3f-cb9c463fa655.gif) @miguelpruivo I have attached my gif which showing the crash in my real device with android 10 or i also tried at android 6.

maxbhoria commented 4 years ago

file_picker

maxbhoria commented 4 years ago

@miguelpruivo you can check this as well now its really difficult to understand whats happening with me

miguelpruivo commented 4 years ago

@maxbhoria what is your device?

maxbhoria commented 4 years ago

Samsung M30s with Android 10.

miguelpruivo commented 4 years ago

@maxbhoria what if you pick a file from other location instead of the Recent folder?

maxbhoria commented 4 years ago

same thing happens every time

miguelpruivo commented 4 years ago

@maxbhoria do you have a different device where you can test it on? Maybe with an emulator?

maxbhoria commented 4 years ago

@miguelpruivo i also tried on micromax device with android 6 but no result.

miguelpruivo commented 4 years ago

Could you please try on the same emulator that I'm using? We need to figure if it's an issue with the build or the device. I'm using Nexus 5X with API 29.

maxbhoria commented 4 years ago

Could you please try on the same emulator that I'm using? We need to figure if it's an issue with the build or the device. I'm using Nexus 5X with API 29.

ok i will check and update you.

maxbhoria commented 4 years ago

@miguelpruivo can we connect on any other platform?

miguelpruivo commented 4 years ago

@maxbhoria don't get me wrong, but I'd rather keep it here so others can keep trace of what's happening if they experience the same issue.

maxbhoria commented 4 years ago

@maxbhoria don't get me wrong, but I'd rather keep it here so others can keep trace of what's happening if they experience the same issue.

@miguelpruivo i am asking for others things like i need to ask some other problem i am facing which is not related to this.