Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flutter downloader is not working in Android 11. #488

Closed
swafvan opened this issue Jul 3, 2021 · 17 comments
Closed

Flutter downloader is not working in Android 11. #488

swafvan opened this issue Jul 3, 2021 · 17 comments

Comments

@swafvan
Copy link

swafvan commented Jul 3, 2021

While I'm able to download and view the file successfully in Android 6 phone, it is not working in Android 11. In Android 11, both log as well as notification says file is downloaded successfully. But in Android 11, file is not actually saved in drive. Please find below code

AndroidManifest.xml

<application
    android:icon="@mipmap/ic_launcher"
    android:label="eCommerce"
    android:networkSecurityConfig="@xml/network_security_config"
    android:requestLegacyExternalStorage="true"
    android:usesCleartextTraffic="true"
    android:hardwareAccelerated="true">
    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
        android:hardwareAccelerated="true"
        android:launchMode="singleTop"
        android:requestLegacyExternalStorage="true"
        android:theme="@style/LaunchTheme"
        android:windowSoftInputMode="adjustResize">
        <!-- Specifies an Android theme to apply to this Activity as soon as
             the Android process has started. This theme is visible to the user
             while the Flutter UI initializes. After that, this theme continues
             to determine the Window background behind the Flutter UI. -->
        <meta-data
            android:name="io.flutter.embedding.android.NormalTheme"
            android:resource="@style/NormalTheme" />
        <!-- Displays an Android View that continues showing the launch screen
             Drawable until Flutter paints its first frame, then this splash
             screen fades out. A splash screen is useful to avoid any visual
             gap between the end of Android's launch screen and the painting of
             Flutter's first frame. -->
        <meta-data
            android:name="io.flutter.embedding.android.SplashScreenDrawable"
            android:resource="@drawable/launch_background" />
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <!-- Don't delete the meta-data below.
         This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
    <meta-data
        android:name="flutterEmbedding"
        android:value="2" />
    <service
        android:name=".MyPrintService"
        android:permission="android.permission.BIND_PRINT_SERVICE">
        <intent-filter>
            <action android:name="android.printservice.PrintService" />
        </intent-filter>
    </service>

    <provider
        android:name="vn.hunghd.flutterdownloader.DownloadedFileProvider"
        android:authorities="${applicationId}.flutter_downloader.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>

    <provider
        android:name="androidx.work.impl.WorkManagerInitializer"
        android:authorities="${applicationId}.workmanager-init"
        android:enabled="false"
        android:exported="false" />

    <provider
        android:name="vn.hunghd.flutterdownloader.FlutterDownloaderInitializer"
        android:authorities="${applicationId}.flutter-downloader-init"
        android:exported="false">
        <meta-data
            android:name="vn.hunghd.flutterdownloader.MAX_CONCURRENT_TASKS"
            android:value="5" />
    </provider>

    <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="${applicationId}.flutter_inappwebview.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>
</application>

<!--    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>-->
<!--    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />

Dart code

final fldr = (Platform.isAndroid
? await getExternalStorageDirectory()
: await getApplicationSupportDirectory());
final taskId = await FlutterDownloader.enqueue(
url: url.toString(),
savedDir: fldr.path,
showNotification: true,
// show download progress in status bar (for Android)
openFileFromNotification:
true,
);

@cbanaspk
Copy link

cbanaspk commented Jul 8, 2021

Any update on this?

@deynerreinoso
Copy link

Any solution yet?

@swafvan
Copy link
Author

swafvan commented Jul 10, 2021

I'm not able to find any solution yet.

@arshadbarves
Copy link

For Flutter_Downloader

If you are using API 29+(ANDROID 10 and Above) add the below code in AndriodManifest.xml

android:requestLegacyExternalStorage="true"

Like this

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">  

    
    ...........
  
    <application
        ..............
        <!-- Add This line -->

        tools:replace="android:label" 
        android:requestLegacyExternalStorage="true"> <!-- Add This line if you are targeting android API 29+-->

        <activity>
            ...............
        </activity>
     
    </application>
</manifest>

@swafvan
Copy link
Author

swafvan commented Jul 11, 2021

As you can see in the first thread, this is already added. It shows downloading and download complete in notification. But there are no files downloaded and cannot open anything from notification. Files are not there in internal storage as well.

@arshadbarves
Copy link

arshadbarves commented Jul 11, 2021

As you can see in the first thread, this is already added. It shows downloading and download complete in the notification. But there are no files downloaded and cannot open anything from notification. Files are not there in internal storage as well.

Confirm the permission before you download it to the device.
Note: Debug the steps on downloading process.

PS. I have implemented it in Android 11.

@swarupbc
Copy link

Same problem for me, it's showing downloaded, but it's not downloaded and not opening from notification panel

@dhavalshah007
Copy link

I am unable to download file, specifically in Realme Phones, otherwise it is working fine for other phones.
I Have found the issue but I don't know how to solve it.

I am unable to find handler for, channel = vn.hunghd/downloader

In _handlers array this key is not there.
Though there are 7 key in _handler array which are as follows:

flutter/restoration
flutter/system
flutter/lifecycle
flutter/keyevent
flutter/navigation
flutter/textinput
plugins.flutter.io/connectivity_status

Where it is working for other phones, over there channel value is vn.hunghd/downloader_background, which is there in the _handler array.

Below is the code of binding.dart inside services folder,

 @override
  Future<void> handlePlatformMessage(
    String channel,
    ByteData? data,
    ui.PlatformMessageResponseCallback? callback,
  ) async {
    ByteData? response;
    try {
      final MessageHandler? handler = _handlers[channel];
      if (handler != null) {
        response = await handler(data);
      } else {
        ui.channelBuffers.push(channel, data, callback!);
        callback = null;
      }
    } catch (exception, stack) {
      FlutterError.reportError(FlutterErrorDetails(
        exception: exception,
        stack: stack,
        library: 'services library',
        context: ErrorDescription('during a platform message callback'),
      ));
    } finally {
      if (callback != null) {
        callback(response);
      }
    }
  }

@swafvan
Copy link
Author

swafvan commented Jul 20, 2021

@arshadbarves , permission is already given. It is not working. I can clearly see app permission Files and Media is there.

@swafvan
Copy link
Author

swafvan commented Jul 22, 2021

It is not working because of a depricated API. I was able to download the file finally. But it is not opening from notification

https://stackoverflow.com/questions/65498961/flutter-get-external-storage-directory-and-mediastore-with-path-provider-package

@jaysavsani07
Copy link

@swafvan I was facing similar issue that on Android 11, after debugging for few days I got it that it's an issue with file name.
from Android 11 filename cannot contain some special characters. So you cannot use forbidden characters like colon (:), forward slash(/), backward slash() etc.
Please check once if it was the problem.

@swafvan
Copy link
Author

swafvan commented Aug 4, 2021 via email

@dhavalshah007
Copy link

I am also able to download files but it is specifically not working for Oppo Phones.

@rahuldubey093
Copy link

rahuldubey093 commented Aug 9, 2021

As you can see in the first thread, this is already added. It shows downloading and download complete in notification. But there are no files downloaded and cannot open anything from notification. Files are not there in internal storage as well.

For Android 11, I am facing the same issues mentioned in the comment above. I have done extensive testing on this thinking it was a flaw in my code but doesn't look like so. I am triple checking for permissions (asking for "MANAGE_EXTERNAL_STORAGE" on Android 11), so this is not related to permissions.

Surprisingly, this issue doesn't happen every time, the same files which couldn't be downloaded now, get successfully downloaded an hour later. Tested only on two OnePlus phone on Android 11.

UPDATE:
This issue is not limited to Android 11, happening on Android 8 on Xiaomi device also.

@swafvan
Copy link
Author

swafvan commented Sep 15, 2021 via email

@sanjidbillah
Copy link

@dhavalshah007 Did you fix oppo problem?
I am facing also.

@alexaung
Copy link

Yes, most of the China phone are not working with this plugin. Any solution yet?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests