flet icon indicating copy to clipboard operation
flet copied to clipboard

Files downloaded via Flet app do not appear in Android file manager apps

Open JudicaelK opened this issue 1 year ago • 7 comments

Duplicate Check

  • [x] I have searched the opened issues and there are no duplicates

Describe the requested feature

Hi,

I'm using a Flet app to download files on my Android device. The download works fine, but I need to restart the phone before the files appear in the "Recent Downloads" or become visible in gallery/file manager apps.

After some research, I found that Android provides a function called MediaScannerConnection.scanFile() in Kotlin/Java to trigger a media rescan.

Is there an equivalent of this in Flet? Or is there any way to force Android to automatically rescan media files after each download from a Flet app?

Thanks in advance!

Suggest a solution

No response

Screenshots

No response

Additional details

No response

JudicaelK avatar Jun 11 '25 22:06 JudicaelK

You can use pyjnius to interact with java classes. Heres a code u can use for that

from jnius import autoclass, cast
import os

# Path to the file you want to scan
file_path = "/sdcard/Download/sample_video.mp4"  # replace with your actual path

# Android classes
PythonActivity = autoclass("org.kivy.android.PythonActivity")
MediaScannerConnection = autoclass("android.media.MediaScannerConnection")
MimeTypeMap = autoclass("android.webkit.MimeTypeMap")
File = autoclass("java.io.File")

# Get current activity context
context = cast("android.content.Context", PythonActivity.mActivity)

# Function to get MIME type if u already have the video details skip this 
def get_mime_type(file_path):
    extension = os.path.splitext(file_path)[1][1:]  # Getting file extension without dot or if u already have details u can fix it manually and skip this
    return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.lower())

# Scan the file from here
def scan_media_file(file_path):
    mime_type = get_mime_type(file_path)
    MediaScannerConnection.scanFile(
        context,
        [file_path],
        [mime_type],
        None  # Optional OnScanCompletedListener
    )

scan_media_file(file_path)

HappyCoder256 avatar Jun 12 '25 00:06 HappyCoder256

@HappyCoder256

I tried it, but it didn't work.

JudicaelK avatar Jun 12 '25 22:06 JudicaelK

Can u send some logs

HappyCoder256 avatar Jun 13 '25 05:06 HappyCoder256

Problem solved, thanks.

JudicaelK avatar Jul 10 '25 02:07 JudicaelK

Can u share ur adjustments for future reference

HappyCoder256 avatar Jul 10 '25 21:07 HappyCoder256

Thanks for following up that it's working for you, @JudicaelK.

Leaving some notes here for anyone who finds this issue by searching.

Why the snippet above didn't work. org.kivy.android.PythonActivity doesn't exist in a Flet app — that's Kivy's class, and Flet uses serious-python. The activity holder is com.flet.serious_python_android.PythonActivity, but don't hardcode it either; look it up via the environment variable, as shown in our Pyjnius blog post:

import os
from jnius import autoclass

activity_host_class = os.getenv("MAIN_ACTIVITY_HOST_CLASS_NAME")
assert activity_host_class
activity = autoclass(activity_host_class).mActivity

But MediaScannerConnection.scanFile() is unlikely to help even with the right class. It only does something for a file in a public, scannable location, and a Flet app can't easily put one there anymore:

  • Every path Flet gives you on Android is app-scoped. StoragePaths.get_downloads_directory() and get_external_storage_directory() resolve to /storage/emulated/0/Android/data/<package>/files/… (they map onto path_provider's getExternalFilesDir), and Android never indexes Android/data in the MediaStore.
  • Since #6740 the Android manifest no longer declares READ_MEDIA_* or *_EXTERNAL_STORAGE — Google Play's Photo and Video Permissions policy rejects apps that request broad media access when the system pickers suffice. With no legacy-storage opt-out, a direct write to /storage/emulated/0/Download/ fails on API 29+ regardless.

What to use instead. To save a file somewhere the user can see it, use FilePicker.save_file() with src_bytes. On Android it goes through the Storage Access Framework, which registers the file with the MediaStore automatically — no scan and no permission needed:

await file_picker.save_file(file_name="video.mp4", src_bytes=data)

To write into the gallery specifically, the modern approach is a MediaStore insert rather than a scan, and that belongs in an extension. @/fazi-gondal built a working one — see the packages/flet-media-scanner/ directory of Vidsaver for a complete Python service + Dart extension + Kotlin MediaStore bridge. An official extension is being tracked in #6648.

ndonkoHenri avatar Sep 04 '26 00:09 ndonkoHenri

Hi @HappyCoder256 @JudicaelK @apumapho

I wanted to share a follow-up to the earlier discussion around media-library support in Flet.

Since the previous issue, I have continued working on this area and have now built a reusable community package:

flet-media-library

It is a Flet service extension for Android and iOS that provides access to the device media library from Python.

The package currently supports:

  • Photos, videos, and Android audio
  • Per-media-type permissions
  • Android 6–12 and Android 13+ permission models
  • Android 14+ selected/limited visual media access
  • iOS PhotoKit full/limited access
  • Albums and media buckets
  • Pagination, filtering, sorting, and date-range queries
  • Media metadata
  • Fast thumbnails
  • Filesystem thumbnail paths to avoid unnecessary Base64 transfer
  • Saving images and videos to the public gallery
  • Saving audio to the Android Music directory
  • Rename and move operations where supported
  • Single and batch deletion
  • Copy support where the platform allows it
  • Live media-library change notifications
  • Platform capability detection
  • Typed exceptions for permission, platform, argument, and asset errors

The implementation uses photo_manager for the cross-platform media-library functionality, with a custom Android Kotlin implementation for functionality such as audio saving and Android-specific MediaStore operations.

The package also includes a working demo application demonstrating gallery browsing, thumbnails, media playback, camera preview, audio recording, gallery saving, moving, renaming, deletion, and other APIs.

Package/repository: https://github.com/fazi-gondal/Flet-media-library

I think this is a useful example of how Flet extensions can provide deeper native platform functionality while keeping the application API Python-friendly.

I am sharing it here as a follow-up to the earlier discussion so people interested in media access, gallery integration, downloads, media applications, or native Flet extensions can see the current implementation and try it.

Feedback, suggestions, and use cases are welcome.

fazi-gondal avatar Sep 22 '26 16:09 fazi-gondal