Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

image_picker closes the app or returns nothing after choosing a photo

ErrorThe picked data never returns to the original call after Android kills MainActivity

Short answer

On Android the system can kill MainActivity while the picker is open, so the app restarts and the result never arrives. Call retrieveLostData at startup. A singleInstance launchMode makes the picker always report a cancel. On iOS, add the usage description keys to Info.plist.

  • Applies to image_picker 1.x, Flutter 3.3x stable, current Android and iOS
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check the activity declaration in the Android manifest and the usage keys in the iOS plist. Both are quick to read and rule out two of the causes.

    grep -n "launchMode" android/app/src/main/AndroidManifest.xml
    grep -n "UsageDescription" ios/Runner/Info.plist
  2. 2Match what you see, then jump to its fix

    1. 1The app is restarted while the picker is open and the picked file never reaches your codeAndroid killed MainActivity while the picker was openFix
    2. 2The picker opens and closes as if you cancelled, every timeMainActivity uses launchMode singleInstanceFix
    3. 3iOS only, and the keys from the plugin README are absent from Info.plistThe iOS usage description keys are not in Info.plistFix

What does the error look like?

  • The picked data never returns to the original call after Android kills MainActivity
  • With launchMode singleInstance the picker always returns RESULT_CANCELED

Cause 1 of 3

Android killed MainActivity while the picker was open

1Check

Android may destroy a background activity to recover memory, and its state is lost. image_picker then cannot return the result to the original call.

grep -rn "retrieveLostData" lib/

2Fix

Ask the plugin for lost data once at startup. The call is one-time, so store the result right away.

Future<void> recoverPickedFiles() async {
  final response = await ImagePicker().retrieveLostData();
  if (response.isEmpty) return;
  final files = response.files;
  if (files != null) {
    // use files
  } else {
    // response.exception has the reason
  }
}

3Verify

After a restart caused by the picker, the picked file is recovered at startup.

flutter run

Confirmed by pub.dev

Did this fix it?

Cause 2 of 3

MainActivity uses launchMode singleInstance

1Check

singleInstance runs the activity alone in its task, and the plugin docs say the picker then always returns RESULT_CANCELED.

grep -n "launchMode" android/app/src/main/AndroidManifest.xml

2Fix

Use singleTask instead, which the plugin recommends.

<activity
    android:name=".MainActivity"
    android:launchMode="singleTask"
    ... >

3Verify

Rebuild and pick a photo. The result now comes back.

flutter run

Confirmed by pub.dev

Did this fix it?

Cause 3 of 3

The iOS usage description keys are not in Info.plist

1Check

The plugin's README requires purpose strings in ios/Runner/Info.plist for the camera, the photo library, and the microphone for video.

plutil -p ios/Runner/Info.plist | grep UsageDescription

2Fix

Add the keys with a short, honest reason for each one that your app uses.

<key>NSPhotoLibraryUsageDescription</key>
<string>Choose a photo for your profile.</string>
<key>NSCameraUsageDescription</key>
<string>Take a photo for your profile.</string>

3Verify

Rebuild the iOS app and pick from the gallery and the camera.

flutter run -d <ios-device-id>

Confirmed by pub.dev

Did this fix it?

How do I stop it coming back?

  • Call retrieveLostData in your first screen's initState on Android.
  • Move camera captures out of the app cache if you need to keep them, since the plugin stores them there.
  • Test the picker on a real Android phone as well as an emulator.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.