AndroidsSystemFileChooser 0.0.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- AndroidsSystemFileChooser-0.0.3.dist-info/LICENSE +21 -0
- AndroidsSystemFileChooser-0.0.3.dist-info/METADATA +89 -0
- AndroidsSystemFileChooser-0.0.3.dist-info/RECORD +8 -0
- AndroidsSystemFileChooser-0.0.3.dist-info/WHEEL +5 -0
- AndroidsSystemFileChooser-0.0.3.dist-info/top_level.txt +1 -0
- androidssystemfilechooser/__init__.py +10 -0
- androidssystemfilechooser/chooser.py +54 -0
- androidssystemfilechooser/utils.py +101 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Mathias Lindström
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
|
+
Name: AndroidsSystemFileChooser
|
|
3
|
+
Version: 0.0.3
|
|
4
|
+
Summary: Android's SystemFileChooser with Kivy
|
|
5
|
+
Home-page: https://github.com/kuzeyron/AndroidsSystemFileChooser
|
|
6
|
+
Author: Mathias Lindström
|
|
7
|
+
License: LICENSE
|
|
8
|
+
Classifier: Intended Audience :: Developers
|
|
9
|
+
Classifier: Topic :: Software Development :: Build Tools
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
|
|
15
|
+
Android's SystemFileChooser with Kivy
|
|
16
|
+
=====================================
|
|
17
|
+
|
|
18
|
+
*A tool that uses Android's SystemFileChooser to process materials with the set of tools from Kivy.*
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
### Buildozer
|
|
22
|
+
To include these tools with Buildozer just include `androidssystemfilechooser` in the requirements.
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### API
|
|
26
|
+
For now it's better if you just read the source/code.
|
|
27
|
+
`JavaStream(input_stream)`
|
|
28
|
+
`SystemFileChooser(mime_type, multiple)`
|
|
29
|
+
`uri_to_extension(uri)`
|
|
30
|
+
`uri_to_filename(uri)`
|
|
31
|
+
`uri_to_stream(uri)`
|
|
32
|
+
`uri_image_to_texture(uri)`
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
### Permissions and Runtime-permissions
|
|
36
|
+
You don't have to state anything.
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
### Example code of use
|
|
40
|
+
```
|
|
41
|
+
from androidssystemfilechooser import (SystemFileChooser,
|
|
42
|
+
uri_image_to_texture,
|
|
43
|
+
uri_to_extension,
|
|
44
|
+
uri_to_stream)
|
|
45
|
+
from kivy.app import App
|
|
46
|
+
from kivy.core.image import Image as CoreImage
|
|
47
|
+
from kivy.lang import Builder
|
|
48
|
+
from kivy.properties import StringProperty
|
|
49
|
+
from kivy.uix.button import Button
|
|
50
|
+
from kivy.uix.image import Image
|
|
51
|
+
|
|
52
|
+
KV = '''
|
|
53
|
+
BoxLayout:
|
|
54
|
+
orientation: 'vertical'
|
|
55
|
+
|
|
56
|
+
MyTriggerButton:
|
|
57
|
+
multiple: True
|
|
58
|
+
text: 'Choose and execute results!'
|
|
59
|
+
on_release: self.trigger()
|
|
60
|
+
'''
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class MyTriggerButton(SystemFileChooser, Button):
|
|
64
|
+
mime_type = StringProperty('image/*')
|
|
65
|
+
|
|
66
|
+
def on_uris(self, instance, uris):
|
|
67
|
+
for nr, uri in enumerate(uris):
|
|
68
|
+
if nr % 2 == 0:
|
|
69
|
+
with uri_to_stream(uri) as stream:
|
|
70
|
+
image = CoreImage(stream, ext=uri_to_extension(uri))
|
|
71
|
+
texture = image.texture
|
|
72
|
+
else:
|
|
73
|
+
texture = uri_image_to_texture(uri)
|
|
74
|
+
instance.parent.add_widget(Image(texture=texture))
|
|
75
|
+
self.uris = []
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class MyApp(App):
|
|
79
|
+
def build(self):
|
|
80
|
+
return Builder.load_string(KV)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
if __name__ == '__main__':
|
|
84
|
+
MyApp().run()
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
### License
|
|
89
|
+
MIT-Licensed.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
androidssystemfilechooser/__init__.py,sha256=NR9xF-XcS-mdKCebW-oG_s7uPVpWAj1MrkkDYBHV66I,344
|
|
2
|
+
androidssystemfilechooser/chooser.py,sha256=MLsgpYGbb9_h9Z-5caX9-KtWiuivSJczZ2c8NPlgPrc,1943
|
|
3
|
+
androidssystemfilechooser/utils.py,sha256=klxOicT6VBlOa1d9twCtfZ4TB3k7cYlqHmwOz4KV4Oc,3084
|
|
4
|
+
AndroidsSystemFileChooser-0.0.3.dist-info/LICENSE,sha256=vO7WrAPOsEI6XC5xa8tBeX6ruTWMbrwffDdvp3ls0mo,1075
|
|
5
|
+
AndroidsSystemFileChooser-0.0.3.dist-info/METADATA,sha256=GyOkTVlPrgJTazjTB7v-CghrBzWRSPJIRCqOsyNPS18,2404
|
|
6
|
+
AndroidsSystemFileChooser-0.0.3.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
|
|
7
|
+
AndroidsSystemFileChooser-0.0.3.dist-info/top_level.txt,sha256=zrpwMhliKoKkJWJE-rypMlDdLZnX_jLBFZBpoQvsdMc,26
|
|
8
|
+
AndroidsSystemFileChooser-0.0.3.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
androidssystemfilechooser
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from .chooser import SystemFileChooser
|
|
2
|
+
from .utils import (JavaStream, uri_image_to_texture, uri_to_extension,
|
|
3
|
+
uri_to_filename, uri_to_stream)
|
|
4
|
+
|
|
5
|
+
__all__ = ('JavaStream',
|
|
6
|
+
'SystemFileChooser',
|
|
7
|
+
'uri_to_extension',
|
|
8
|
+
'uri_to_filename',
|
|
9
|
+
'uri_to_stream',
|
|
10
|
+
'uri_image_to_texture')
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
from android import activity, mActivity
|
|
2
|
+
from jnius import autoclass
|
|
3
|
+
from kivy.event import EventDispatcher
|
|
4
|
+
from kivy.logger import Logger
|
|
5
|
+
from kivy.properties import BooleanProperty, ListProperty, StringProperty
|
|
6
|
+
|
|
7
|
+
__all__ = ('SystemFileChooser', )
|
|
8
|
+
|
|
9
|
+
Intent = autoclass('android.content.Intent')
|
|
10
|
+
REQUEST_CODE_OPEN_DOCUMENT = 1
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class SystemFileChooser(EventDispatcher):
|
|
14
|
+
'''Class to be used as a object or subclassed.'''
|
|
15
|
+
|
|
16
|
+
mime_type = StringProperty('*/*')
|
|
17
|
+
'''Mime type to be used with SystemFileChooser.'''
|
|
18
|
+
|
|
19
|
+
multiple = BooleanProperty(False)
|
|
20
|
+
'''Multiple selection with SystemFileChooser.'''
|
|
21
|
+
|
|
22
|
+
uris = ListProperty()
|
|
23
|
+
'''Collection of URI's coming from System's own FileChooser.'''
|
|
24
|
+
|
|
25
|
+
def trigger(self):
|
|
26
|
+
'''Triggers the System's own FileChooser.'''
|
|
27
|
+
activity.bind(on_activity_result=self._chooser_results)
|
|
28
|
+
intent = Intent(Intent.ACTION_GET_CONTENT)
|
|
29
|
+
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, self.multiple)
|
|
30
|
+
intent.setType(self.mime_type)
|
|
31
|
+
mActivity.startActivityForResult(intent, REQUEST_CODE_OPEN_DOCUMENT)
|
|
32
|
+
|
|
33
|
+
def _chooser_results(self, requestcode, resultcode, intent):
|
|
34
|
+
'''Activity results triggered by the trigger itself.'''
|
|
35
|
+
activity.unbind(on_activity_result=self._chooser_results)
|
|
36
|
+
|
|
37
|
+
if resultcode != -1 or intent is None:
|
|
38
|
+
return # return nothing because no files selected
|
|
39
|
+
|
|
40
|
+
uris = []
|
|
41
|
+
|
|
42
|
+
if requestcode == REQUEST_CODE_OPEN_DOCUMENT and resultcode == -1:
|
|
43
|
+
try:
|
|
44
|
+
if (clip_data := intent.getClipData()) is not None:
|
|
45
|
+
for i in range(clip_data.getItemCount()):
|
|
46
|
+
uris.append(clip_data.getItemAt(i).getUri())
|
|
47
|
+
|
|
48
|
+
elif (clip_data := intent.getData()) is not None:
|
|
49
|
+
uris.append(clip_data)
|
|
50
|
+
|
|
51
|
+
self.uris.extend(uris)
|
|
52
|
+
|
|
53
|
+
except Exception as e:
|
|
54
|
+
Logger.error('Error getting file/s: %s', e)
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
from io import RawIOBase
|
|
2
|
+
|
|
3
|
+
from android import mActivity
|
|
4
|
+
from jnius import autoclass
|
|
5
|
+
from kivy.graphics.texture import Texture
|
|
6
|
+
|
|
7
|
+
__all__ = ('JavaStream',
|
|
8
|
+
'uri_to_extension',
|
|
9
|
+
'uri_to_filename',
|
|
10
|
+
'uri_to_stream',
|
|
11
|
+
'uri_image_to_texture')
|
|
12
|
+
|
|
13
|
+
BitmapFactory = autoclass('android.graphics.BitmapFactory')
|
|
14
|
+
BitmapFactoryOptions = autoclass('android.graphics.BitmapFactory$Options')
|
|
15
|
+
ByteBuffer = autoclass('java.nio.ByteBuffer')
|
|
16
|
+
Intent = autoclass('android.content.Intent')
|
|
17
|
+
MediaColumns = autoclass('android.provider.MediaStore$MediaColumns')
|
|
18
|
+
MimeTypeMap = autoclass('android.webkit.MimeTypeMap')
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class JavaStream(RawIOBase):
|
|
22
|
+
'''Making the stream readable.'''
|
|
23
|
+
|
|
24
|
+
def __init__(self, input_stream):
|
|
25
|
+
self._stream = input_stream
|
|
26
|
+
|
|
27
|
+
def close(self) -> None:
|
|
28
|
+
self._stream.close()
|
|
29
|
+
super().close()
|
|
30
|
+
|
|
31
|
+
def readall(self) -> bytes:
|
|
32
|
+
self._checkClosed()
|
|
33
|
+
buffer_size = self._stream.available()
|
|
34
|
+
buffer = bytearray(buffer_size)
|
|
35
|
+
read_bytes = self.readinto(buffer)
|
|
36
|
+
return bytes(buffer[:read_bytes])
|
|
37
|
+
|
|
38
|
+
def readinto(self, buffer) -> int | None:
|
|
39
|
+
self._checkClosed()
|
|
40
|
+
read_bytes = self._stream.read(buffer)
|
|
41
|
+
|
|
42
|
+
if read_bytes < 0: # EOF
|
|
43
|
+
return 0
|
|
44
|
+
|
|
45
|
+
return read_bytes
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def uri_to_extension(uri):
|
|
49
|
+
'''Returns the extension name from the filename using URI.'''
|
|
50
|
+
return MimeTypeMap.getFileExtensionFromUrl(uri_to_filename(uri))
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def uri_to_filename(uri):
|
|
54
|
+
'''Returns the filename from URI. On newer versions it doesn't return the
|
|
55
|
+
correct filename. But you will get the correct extension name.'''
|
|
56
|
+
cursor = mActivity.getContentResolver().query(uri, None, None, None, None)
|
|
57
|
+
|
|
58
|
+
if cursor is not None and cursor.moveToFirst():
|
|
59
|
+
filename_index = cursor.getColumnIndex(MediaColumns.DISPLAY_NAME)
|
|
60
|
+
filename = cursor.getString(filename_index) if filename_index != -1 else None
|
|
61
|
+
cursor.close()
|
|
62
|
+
return filename
|
|
63
|
+
|
|
64
|
+
return uri.getLastPathSegment()
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def uri_to_image(uri):
|
|
68
|
+
'''Open bitmap with the use of URI.'''
|
|
69
|
+
context = mActivity.getApplicationContext()
|
|
70
|
+
resolver = context.getContentResolver()
|
|
71
|
+
stream = resolver.openInputStream(uri)
|
|
72
|
+
bitmap = BitmapFactory.decodeStream(stream)
|
|
73
|
+
|
|
74
|
+
return bitmap
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def uri_to_stream(uri):
|
|
78
|
+
'''Opens by URI the FileInputStream with JavaStream.'''
|
|
79
|
+
resolver = mActivity.getContentResolver()
|
|
80
|
+
stream = resolver.openInputStream(uri)
|
|
81
|
+
|
|
82
|
+
return JavaStream(stream)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def uri_image_to_texture(uri):
|
|
86
|
+
'''Using URI to load the selected image and convert
|
|
87
|
+
the image to pixels. Returns texture and size.'''
|
|
88
|
+
bitmap = uri_to_image(uri)
|
|
89
|
+
bitmap_size = bitmap.getWidth(), bitmap.getHeight()
|
|
90
|
+
|
|
91
|
+
buffer = ByteBuffer.allocateDirect(bitmap.getByteCount())
|
|
92
|
+
bitmap.copyPixelsToBuffer(buffer)
|
|
93
|
+
bitmap.recycle()
|
|
94
|
+
|
|
95
|
+
texture = Texture.create(bitmap_size, colorfmt='rgba')
|
|
96
|
+
texture.blit_buffer(bytes(buffer.array()),
|
|
97
|
+
colorfmt='rgba',
|
|
98
|
+
bufferfmt='ubyte')
|
|
99
|
+
texture.flip_vertical()
|
|
100
|
+
|
|
101
|
+
return texture
|