Skip to content

Commit

Permalink
Merge pull request #754 from DexerBR/patch-2
Browse files Browse the repository at this point in the history
Fixed issue accessing sd_card path in newer Android APIs
  • Loading branch information
akshayaurora authored Jul 14, 2023
2 parents d94d5f9 + 9b65ab4 commit d8a2b3d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 34 deletions.
29 changes: 10 additions & 19 deletions plyer/platforms/android/filechooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
.. versionadded:: 1.4.0
'''

from os.path import join, basename
from os.path import join
from random import randint

from android import activity, mActivity
Expand Down Expand Up @@ -253,28 +253,19 @@ def _handle_external_documents(uri):
file_id = DocumentsContract.getDocumentId(uri)
file_type, file_name = file_id.split(':')

# internal SD card mostly mounted as a files storage in phone
internal = storagepath.get_external_storage_dir()
primary_storage = storagepath.get_external_storage_dir()
sdcard_storage = storagepath.get_sdcard_dir()

# external (removable) SD card i.e. microSD
external = storagepath.get_sdcard_dir()
try:
external_base = basename(external)
except TypeError:
external_base = basename(internal)
directory = primary_storage

# resolve sdcard path
sd_card = internal

# because external might have /storage/.../1 or other suffix
# and file_type might be only a part of the real folder in /storage
if file_type in external_base or external_base in file_type:
sd_card = external
if file_type == "primary":
directory = primary_storage
elif file_type == "home":
sd_card = join(Environment.getExternalStorageDirectory(
).getAbsolutePath(), Environment.DIRECTORY_DOCUMENTS)
directory = join(primary_storage, Environment.DIRECTORY_DOCUMENTS)
elif sdcard_storage and file_type in sdcard_storage:
directory = sdcard_storage

return join(sd_card, file_name)
return join(directory, file_name)

@staticmethod
def _handle_media_documents(uri):
Expand Down
34 changes: 19 additions & 15 deletions plyer/platforms/android/storagepath.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
--------------------
'''

from os import listdir, access, R_OK
from os.path import join
from plyer.facades import StoragePath
from jnius import autoclass
from jnius import autoclass, cast
from android import mActivity

Environment = autoclass('android.os.Environment')
Context = autoclass('android.content.Context')
Environment = autoclass("android.os.Environment")
Context = autoclass("android.content.Context")


class AndroidStoragePath(StoragePath):
Expand All @@ -25,17 +23,23 @@ def _get_sdcard_dir(self):
'''
.. versionadded:: 1.4.0
'''
# folder in /storage/ that is readable
# and is not internal SD card
path = None
for folder in listdir('/storage'):
folder = join('/storage', folder)
if folder in self._get_external_storage_dir():
continue
if not access(folder, R_OK):
continue
path = folder
break
context = mActivity.getApplicationContext()
storage_manager = cast(
"android.os.storage.StorageManager",
context.getSystemService(Context.STORAGE_SERVICE),
)

if storage_manager is not None:
storage_volumes = storage_manager.getStorageVolumes()
for storage_volume in storage_volumes:
if storage_volume.isRemovable():
try:
directory = storage_volume.getDirectory()
except AttributeError:
directory = storage_volume.getPathFile()
path = directory.getAbsolutePath()

return path

def _get_root_dir(self):
Expand Down

0 comments on commit d8a2b3d

Please sign in to comment.