Update: When I use your code, my phone will immediately show picture that I downloaded, so Maybe it is
- Your phone gallery cache problem.
- Your filepath or filename was wrong.
- You putted your file in the path that will not show media.
- Check if there is ".nomedia" file in your path or not, and delete it.
PythonActivity = autoclass('org.kivy.android.PythonActivity')
activity = PythonActivity.mActivity
currentActivity = cast('android.app.Activity', activity)
currentApplication = currentActivity.getApplicationContext()
Context = cast('android.content.Context', currentApplication)
MediaScannerConnection = autoclass('android.media.MediaScannerConnection')
def android_rescan_MediaStore(file):
if platform=="android":
MediaScannerConnection.scanFile(self.Context, [file], None, None)
To refresh the single file in MediaStore on Android, I use this way: (Test on Galaxy A8 2018.)
If this didn't work, try to clean your music player、gallery cache.
from jnius import autoclass, cast
PythonActivity = autoclass('org.kivy.android.PythonActivity')
activity = PythonActivity.mActivity
currentActivity = cast('android.app.Activity', activity)
File = autoclass('java.io.File')
Uri = autoclass('android.net.Uri')
Intent = autoclass('android.content.Intent')
Context = cast('android.content.Context', currentActivity.getApplicationContext())
def android_rescan_MediaStore(file):
file = Uri.fromFile(File(file))
mediaScanIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, file)
Context.sendBroadcast(mediaScanIntent)
For example:
android_rescan_MediaStore("/storage/emulated/0/Download/example.mp3")
To find file path in primary external storage:
import os
from android.storage import primary_external_storage_path
from android.permissions import request_permissions, Permission
request_permissions([Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE])
AndroidPath = primary_external_storage_path()
filename = os.path.join(AndroidPath, "You file name and path in primary external storage")
# And then refresh it
android_rescan_MediaStore(filename)
# References:
# Stackoverflow tell me: You need at least 10 reputation to post more than 8 links.
# My reputation<10, so I use this way to post.
# https://stackoverflow.com/questions/72586638/downloaded-files-not-appearing-in-the-downloads-application-in-android-kivy
# https://developer.android.com/reference/android/content/Intent
# https://pyjnius.readthedocs.io/en/stable/android.html
# https://stackoverflow.com/questions/3572463/what-is-context-on-android
# https://developer.android.com/reference/android/content/Context
# https://developer.android.com/reference/android/content/package-summary
# https://stackoverflow.com/questions/4646913/android-how-to-use-mediascannerconnection-scanfile
# https://codertw.com/android-%E9%96%8B%E7%99%BC/331396/
# https://segmentfault.com/a/1190000014593444
# https://stackoverflow.com/questions/69054442/kivy-python-android-app-why-downloaded-video-from-any-website-are-not-showing-in
# https://stackoverflow.com/questions/54442336/using-mediastore-in-kivy-with-pyjnius
# https://developer.android.com/reference/android/provider/MediaStore
# https://stackoverflow.com/questions/3300137/how-can-i-refresh-mediastore-on-android
# https://stackoverflow.com/questions/60203353/action-media-scanner-scan-filestring-is-deprecated/63413716
# https://www.google.com/search?q=Android+Media+Storage
# https://www.google.com/search?q=Android%20rescan%20media