A am working on an Android app which connects to a local flask server using WiFi. Then, the app displays images which are stored on the server (RPi3). A click on an image triggers a download request and I want the Android DownloadManager to enqueue the request and download the image. The WiFi network does not provide internet access.
So far, I have been able to test it on Android 6 and Android 8.1 devices and everything works fine. Testing on several Android 9 devices, the download does not start, but after disconnecting from the local network the failed attempts are shown.
Reading other threads about this, I have tried the following:
- Setting the network security config as described here: Download Manger not working in Android Pie 9.0 (Xiaomi mi A2)
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
And added in the AndroidManifest.xml:
android:networkSecurityConfig="@xml/network_security_config"
Binding the network as described in the first answer here: Using a WiFi without Internet Connection
Playing with the options the DownloadManager.Request class provides, but they do not seem related to this problem:
.setRequiresCharging(false)
.setAllowedOverMetered(false)
.setAllowedOverRoaming(false)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, (String) url);
- Checking again permissions. These are enabled:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
This is the download request I create:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url))
.setTitle("Some Title")
.setDescription("Downloading a file")
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalPublicDir("some_path", (String) url)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
DownloadManager downloadManager= (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
downloadID = downloadManager.enqueue(request);
The Flask server (Python3) uses this the return the image:
@app.route('/getFullImage/<image_file>')
def get_full_image(image_file):
log.debug("Client requests full image.")
folder = os.path.join(app.root_path, "media_images")
return flask.send_from_directory(directory=folder, filename=image_file, as_attachment=True)
As said, on Android 6 and 8.1, it worked fine but none of the Android 9 devices start the download. If there is any setting or method I do not know of, I would be happy to learn about it.
Thank you.