storage android version 10 and above .

116 views
Skip to first unread message

purushottam yadav

unread,
Nov 14, 2022, 12:49:25 PM11/14/22
to Kivy users support

These below codes worked  on  android 9 but not working no 10 and above . 

 I tried to solve the issue using https://github.com/Android-for-Python/Android-for-Python-Users#android-storage  , https://github.com/Android-for-Python/Android-for-Python-Users#android-storage  ,  https://github.com/Android-for-Python/shared_storage_example  ,  https://github.com/Android-for-Python/androidstorage4kivy  .

but  i was not able to find out , so i am asking here .

first  takingper Requests permissions on Android and calls  self.loadaddress()  . this  function just calls  get__images(self)    and get__images(self)   stores all images paths and its thumbnails paths in  a list   self.lst_thumbnails_images.append( {'Imgsource': imagecursor.getString(dataColumnIndex) , 'source' : self.getThumbnailPath(imagecursor.getString(dataColumnIndex)) }  )   ,  here  self.getThumbnailPath() function is used to get the path of thumbnails   ,  it checks if thumbnail already exists for the image path , if not it calls   def thumforpic(self, path):  to create one  and  returns the path  .

 def takingper(self):  ,  self.loadaddress()    ,   def get__images(self) :   are working fine on android 10 . 
but   self.getThumbnailPath()  ,  def thumforpic(self, path):    are working as expected .

issues are  :

1) self.getThumbnailPath()  is not creating  a folder "INETWORK"   and 
2) def thumforpic(self, path)   is not creating thumbnail  and 
3) i also want to display those images  .



    def takingper(self):
        # only  Request permissions on Android , not loading images path
        if platform == 'android' :
            from android.permissions import Permission, request_permissions


            def call__back(permission, results):
                if all([res for res in results]):
                    print("Got all permissions>>>>>>>>>>>>>>>>>>>>>>>")
                    self.per = True
                    self.loadaddress()
                    print("called loadaddress function ======================")

                else:
                    print("Did not get all permissions")
                    self.per = False

            request_permissions([Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE], call__back)
        else:
            self.per = True
            self.loadaddress()












    def loadaddress(self) :

        print("Instance created ")

        if platform == 'android' :
            print("loading....=============================")
            all_media = False

            Thread(target= self.get__images ).start()
                 
            self.imagepicscreen = 'ready'
        

    def get__images(self) :
        lst_only_get__images = []

        IMMedia = autoclass('android.provider.MediaStore$Images$Media')

        columns = [IMMedia.DATA, IMMedia._ID ] #;//get all columns of type images
        orderBy =  IMMedia.DATE_TAKEN   # JString("datetaken") # ;//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
            lst_only_get__images.append( imagecursor.getString(dataColumnIndex) )

            self.lst_thumbnails_images.append( {'Imgsource': imagecursor.getString(dataColumnIndex) , 'source' : self.getThumbnailPath(imagecursor.getString(dataColumnIndex)) }  )


            imagecursor.moveToNext()



        print( imagecursor.getCount() )
        print(i)
       
        self.done_media = True
     










    def  getThumbnailPath(self ,   path):

        #video_path, output_path='', custom_text=True, font_dir='', font_size=
        result = os.path.basename(path)[:-3] + "jpg"
        #print(str(path)*(10))
            MediaStore = autoclass('android.provider.MediaStore$MediaColumns')
            thumb = autoclass('android.provider.MediaStore$Images$Thumbnails')
            IMMedia = autoclass('android.provider.MediaStore$Images$Media')
            imageId = -1
            projection = MediaStore._ID
            selection = MediaStore.DATA + "=?"
            selectionArgs = path
            cursor = ContentResolver.getContentResolver().query(IMMedia.EXTERNAL_CONTENT_URI, [projection], selection, [selectionArgs] , None)
            #print("cursor to imageid>>>>>>>>>>>>>>>>>>>>>>>" , cursor)
            if (cursor != None and cursor.moveToFirst()):
                imageId = cursor.getInt(cursor.getColumnIndex(MediaStore._ID))
                #print("imageId >>>>>>>>>>>>>>>" , imageId )
                cursor.close()
            result = None




            columns_to_return = thumb.DATA
            where = thumb.IMAGE_ID +" LIKE ?"

            valuesAre = "%"+ str(imageId )
            cursor = ContentResolver.getContentResolver().query(thumb.EXTERNAL_CONTENT_URI, [columns_to_return] , where, [valuesAre] , None)



            #cursor = thumb.queryMiniThumbnail(ContentResolver.getContentResolver(), imageId, thumb.MINI_KIND, None)
            #print("cursor to image>>>>>>>>>>>>>>>>>>>> " , cursor  , cursor.getCount()  )
            if (cursor != None and cursor.getCount() > 0) :

                cursor.moveToFirst()
                result = cursor.getString(cursor.getColumnIndexOrThrow(thumb.DATA))
                cursor.close()
                print("result >>>>>>>>>>>>>>>>>" , result  , path )

            else :
                print("no thumbnail" , path )

                #return path
                
                #Cr.insert( Uri.fromFile(File(path) ) , None )
                #video_path, output_path='', custom_text=True, font_dir='', font_size=
                from android.storage import app_storage_path , primary_external_storage_path


                app_storage_directory_path = app_storage_path()
                from android import api_version
                print(api_version)
               
                if  api_version < 29:
                    root = primary_external_storage_path()
                else :
                    root = app_storage_path()

                dir = join(root, Environment.DIRECTORY_DCIM)

                print("dir  >> " , dir )

               
                if  api_version < 29  :
                    dir = join(dir , "INETWORK" )
                    if not exists(dir):
                        mkdir(dir)

                # creating thumbnail
                result =   join( dir ,  "thu" + os.path.basename(path)[:-3] + "jpg"  )
                print(result)

                #print(result)
                if not exists(result):
                    self.thumforpic(path)
                    print("creating " , result )


                else :
                    print("exists already"   , result )


            #print(os.listdir())

            return result










    def thumforpic(self, path):


        #print(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3])

        # creating a object
        from PIL import Image as Ima
        image = Ima.open(path )
        MAX_SIZE = (100, 100)

        from android.storage import app_storage_path , primary_external_storage_path

        app_storage_directory_path = app_storage_path()

        image.thumbnail(MAX_SIZE)
        from android import api_version

        if  api_version < 29:
            root = primary_external_storage_path()
        else :
            root = app_storage_path()

        dir = join(root, Environment.DIRECTORY_DCIM)

        print("dir  >> " , dir )

        if not exists(dir):
            print("creating $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ folder ")
            mkdir(dir)

        if  api_version < 29:
            dir = join(dir , "INETWORK" )
            if not exists(dir):
                mkdir(dir)

        # creating thumbnail
        result =   join( dir ,  "thu" + os.path.basename(path)[:-3] + "jpg"  )
        #print(result)
        image.save( result ) #   str( app_storage_directory_path )   + "thu" + os.path.basename(path)[:-3] + "jpg")

        #print(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3])
        #print(os.listdir())


Robert

unread,
Nov 14, 2022, 8:25:03 PM11/14/22
to Kivy users support
I have never tried to use thumbnails so I have no code or experience I can point you to.

Hard to read the above but it looks like it tries to save thumbnails is a directory on all Android versions (I may have mis-understood), this is not going to work on 10+ , there is no visible file system see https://github.com/Android-for-Python/Android-for-Python-Users#shared-storage

Either these thumbnails must be saved in the MediaStore, or maybe (in the back of my head I think I read somewhere) the MediaStore will create thumbnails for you. In either case you can access thumbnails from the MediaStore, for a Python app I think 'access' means 'copy' (there may be a better way, I have never looked for it).


Reply all
Reply to author
Forward
0 new messages