SAF(Storage Access Framework) in pyjnius in python for Android 10 and higher

33 views Asked by At

I am using Pydroid3. I am making an practice app in which a button will clicked then SAF will open to choose image. Image will select by user and selected image will show.

But image is not showing, why? I don't know. Code is not making any error. But after selecting an image SAF remove but image doesn't show if you say for permission then permission already given to my python ide.

from kivymd.app import MDApp
from kivy.uix.button import Button
from kivy.uix.image import Image
from jnius import autoclass,cast
from kivymd.uix.snackbar import Snackbar
from kivy.uix.boxlayout import BoxLayout

class ImagePickerApp(MDApp):
    def build(self):
        layout = self.create_layout()
        return layout

    def create_layout(self):
        layout = BoxLayout(orientation='vertical', spacing=10, padding=10)
        
        image = Image(source='default_image.png')
        pick_image_button = Button(text='Pick Image', on_press=self.pick_image)
        
        layout.add_widget(image)
        layout.add_widget(pick_image_button)

        return layout

    def pick_image(self, instance):
        # Use jnius to access Android Intent for picking images
        PythonActivity = autoclass('org.kivy.android.PythonActivity')
        Intent = autoclass('android.content.Intent')
        Uri = autoclass('android.net.Uri')

        intent = Intent()
        intent.setAction(Intent.ACTION_GET_CONTENT)
        intent.setType("image/*")

        current_activity = cast('android.app.Activity', PythonActivity.mActivity)
        current_activity.startActivityForResult(intent, 0) # This is returning None
        
    def on_activity_result(self, requestCode, resultCode, intent):
        # Handle the result of the image picking
        if requestCode == 0 and resultCode == -1:
            # -1 means RESULT_OK
            image_uri = intent.getData()
            # Process the selected image URI as needed
            # Update the displayed image, for example:
            self.root.children[0].source =str( image_uri)

    def snackbar(self, message):
        Snackbar(text=message).open()
if __name__ == '__main__':
    ImagePickerApp().run()

How to solve this problem?

0

There are 0 answers