Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed reading file without external storage, added reading file from … #611

Closed
wants to merge 8 commits into from
3 changes: 2 additions & 1 deletion plyer/platforms/android/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def _send(self, **kwargs):
Intent.createChooser(intent, chooser_title)
)
else:
activity.startActivity(intent)
intent.setClassName('com.google.android.gm', 'com.google.android.gm.ComposeActivityGmailExternal')

kengoon marked this conversation as resolved.
Show resolved Hide resolved


def instance():
Expand Down
55 changes: 44 additions & 11 deletions plyer/platforms/android/filechooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
from plyer.facades import FileChooser
from plyer import storagepath


String = autoclass('java.lang.String')
Intent = autoclass('android.content.Intent')
Activity = autoclass('android.app.Activity')
Expand All @@ -78,6 +77,25 @@ class AndroidFileChooser(FileChooser):
# default selection value
selection = None

# mime types
mime_type = {
"doc": "application/msword",
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"ppt": "application/vnd.ms-powerpoint",
"pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
"xls": "application/vnd.ms-excel",
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"text": "text/*",
"pdf": "application/pdf",
"zip": "application/zip",
"image": "image/*",
"video": "video/*",
"audio": "audio/*",
"application": "application/*"
}

selected_mime_type = None

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.select_code = randint(123456, 654321)
Expand Down Expand Up @@ -110,10 +128,15 @@ def _open_file(self, **kwargs):
self._handle_selection = kwargs.pop(
'on_selection', self._handle_selection
)
self.selected_mime_type = kwargs.pop("filters")[0]

# create Intent for opening
file_intent = Intent(Intent.ACTION_GET_CONTENT)
file_intent.setType('*/*')
if not self.selected_mime_type or type(self.selected_mime_type) != str or\
self.selected_mime_type not in self.mime_type:
file_intent.setType("*/*")
else:
file_intent.setType(self.mime_type[self.selected_mime_type])
file_intent.addCategory(
Intent.CATEGORY_OPENABLE
)
Expand Down Expand Up @@ -168,7 +191,10 @@ def _handle_external_documents(uri):

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

# resolve sdcard path
sdcard = internal
Expand All @@ -178,8 +204,7 @@ def _handle_external_documents(uri):
if file_type in external_base or external_base in file_type:
sdcard = external

path = join(sdcard, file_name)
return path
return join(sdcard, file_name)

@staticmethod
def _handle_media_documents(uri):
Expand Down Expand Up @@ -307,9 +332,11 @@ def _resolve_uri(self, uri):
selection = None
downloads = None

# This does not allow file selected from google photos or gallery
# or even any other file explorer to work
# not a document URI, nothing to convert from
if not DocumentsContract.isDocumentUri(mActivity, uri):
return path
# if not DocumentsContract.isDocumentUri(mActivity, uri):
# return path

if uri_authority == 'com.android.externalstorage.documents':
return self._handle_external_documents(uri)
Expand All @@ -325,10 +352,16 @@ def _resolve_uri(self, uri):

# parse content:// scheme to path
if uri_scheme == 'content' and not downloads:
path = self._parse_content(
uri=uri, projection=['_data'], selection=selection,
selection_args=[file_name], sort_order=None
)
try:
path = self._parse_content(
uri=uri, projection=['_data'], selection=selection,
selection_args=file_name, sort_order=None
)
except JavaException: # handles array error for selection_args
path = self._parse_content(
uri=uri, projection=['_data'], selection=selection,
selection_args=[file_name], sort_order=None
)

# nothing to parse, file:// will return a proper path
elif uri_scheme == 'file':
Expand Down