list of images in android device sort by date

289 views
Skip to first unread message

purushottam yadav

unread,
Sep 17, 2022, 9:39:23 AM9/17/22
to Kivy users support
I need the list of all images in android phone , they all need to be sorted by date of modified  or added date  .

i am looking for codes in java using mediastore  and trying to implement  it in pyjnius .

no success till now , please help me out .

Robert

unread,
Sep 17, 2022, 12:54:51 PM9/17/22
to Kivy users support
If the question is about accessing the MediaStore from Python, look at the code in AndroidStorage4Kivy
https://github.com/Android-for-Python/androidstorage4kivy/blob/main/src/androidstorage4kivy/sharedstorage.py

If the question is about querying a date, you'll have to query for DATE_MODIFIED or similar. I don't have an example, but I expect there are Java examples on StackExchange or similar.

purushottam yadav

unread,
Sep 19, 2022, 3:54:13 AM9/19/22
to Kivy users support
i made two cursors  imagecursor  to get images  sort by date time , Vcursor  to get videos  sort by date time .
but i need a single cursor to get both images and videos sort by date time .
i tried to merge those cursors to get new one , but it is not working as expected , newcursor  is first going through all  images and next  all videos  .

please help me out .





from kivy.utils import platform

if platform == "android":
    from jnius import autoclass
    from toast import Toast
    thuu = autoclass('android.media.ThumbnailUtils')
    pythonActivity = autoclass('org.kivy.android.PythonActivity')
    ContentResolver = pythonActivity.mActivity
    Jint = autoclass('java.lang.Integer')
    Long = autoclass('java.lang.Long')
    JString = autoclass('java.lang.String')
    Context = autoclass('android.content.ContentResolver')
    File = autoclass('java.io.File')
    Uri = autoclass('android.net.Uri')
    Cr = autoclass('android.content.ContentResolver')




    def get__images(self) :
        IMMedia = autoclass('android.provider.MediaStore$Images$Media')

        print("images are loading ")
        columns = [IMMedia.DATA, IMMedia._ID ] #;//get all columns of type images
        orderBy = IMMedia.DATE_TAKEN   #;//order data by date
        imagecursor = ContentResolver.getContentResolver().query(
            IMMedia.EXTERNAL_CONTENT_URI, columns, None ,
            None , orderBy + " DESC") #;//get all data in Cursor by sorting in DESC order

        imagecursor.moveToFirst()

        i = 0

        while not  imagecursor.isAfterLast()  :
            print(i)
            i = i + 1
            dataColumnIndex = imagecursor.getColumnIndex(IMMedia.DATA) #;//get column index
            print( imagecursor.getString(dataColumnIndex) ) #;//get Image from column index

            imagecursor.moveToNext()

            print("++++++")

        print( imagecursor.getCount() )
        print(i)
        print("=====================================================================================================")


        self.get__videos(i ,  imagecursor )




    def get__videos(self , i  , imagecursor ) :

        VMedia = autoclass('android.provider.MediaStore$Video$Media') # similarly for videos .

        print("images are loading ")
        columns = [VMedia.DATA, VMedia._ID ] #;//get all columns of type images
        orderBy = VMedia.DATE_TAKEN   #;//order data by date
        Vcursor = ContentResolver.getContentResolver().query(
            VMedia.EXTERNAL_CONTENT_URI, columns, None ,
            None , orderBy + " DESC") #;//get all data in Cursor by sorting in DESC order

        Vcursor.moveToFirst()

        v = 0

        while not  Vcursor.isAfterLast()  :
            print(v)
            v = v + 1
            dataColumnIndex = Vcursor.getColumnIndex(VMedia.DATA) #;//get column index
            print( Vcursor.getString(dataColumnIndex) ) #;//get Image from column index

            Vcursor.moveToNext()

            print("++++++")

        print( Vcursor.getCount() )
        print(v)
        print(i + v , "  <<< i  and v  ")

        MergeCursor =   autoclass('android.database.MergeCursor')
        newcursor =  MergeCursor([imagecursor , Vcursor ])

        newcursor.moveToFirst()

        t = 0
        while not  newcursor.isAfterLast()  :
            print(t)
            t  = t  + 1
            dataColumnIndex = newcursor.getColumnIndex(VMedia.DATA) #;//get column index
            print( newcursor.getString(dataColumnIndex) ) #;//get Image from column index

            newcursor.moveToNext()

            print("++++++")

        print( newcursor.getCount() )
        print( t  , " <<< t ")
        print(i + v , "  <<< i  and v  ")



Robert

unread,
Sep 19, 2022, 12:53:12 PM9/19/22
to Kivy users support
Sorry, I have not done what you want to do.
Reply all
Reply to author
Forward
0 new messages