0

my question is I build a android app using kivy and python for video downloader but there is a problem ,problem is when i downloading any video from any website that video immediately not showing in my android mobile media gallery my mobile model is honor 8x and real me6 , even i used to rescan MediaScannerConnection to scan video but not showing downloaded video in android gallery immediately,what is the reason? or tell me how to do in python thanks in advance.

 from jnius import autoclass, cast
 PythonActivity = autoclass('org.kivy.android.PythonActivity')
 currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
 context = cast('android.content.Context', currentActivity.getApplicationContext())
 Intent = autoclass('android.content.Intent')
 Uri = autoclass('android.net.Uri')
 MediaScannerConnection = autoclass('android.media.MediaScannerConnection')
 MediaScannerConnection.scanFile(context,[path],None, None)
Abdul
  • 11
  • 2
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Sep 08 '21 at 12:32
  • @Community my question is I build a android app using kivy and python for video downloader but there is a problem ,problem is when i downloading any video from any website that video immediately not showing in my android mobile media gallery my mobile model is honor 8x and real me 6 , even i used to rescan MediaScannerConnection to scan video but not showing downloaded video in android gallery immediately,what is the reason? – Abdul Sep 09 '21 at 10:08

1 Answers1

0

Update: When I use your code, my phone will immediately show picture that I downloaded, so Maybe it is

  1. Your phone gallery cache problem.
  2. Your filepath or filename was wrong.
  3. You putted your file in the path that will not show media.
  4. 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
謝咏辰
  • 37
  • 4