Home » Java » Java – Android file chooser on kivy. Python

Java – Android file chooser on kivy. Python

Posted by: admin June 30, 2018 Leave a comment

Questions:

I’m trying to multi-select files on android file chooser, but it returns None. I think the error is from line 29. The code works if your want the uri but I need the filepath for a kivy image widget or soundloader. Any help will be appreciated because I’m willing to learn. Thank you!

def select_media(self, callback, mood):
    type0 = ''
    if 'image' in mood: type0 = "image/*"
    if 'audio' in mood: type0 = "audio/*"
    if 'vid' in mood: type0 = "video/*"

    currentActivity = cast('android.app.Activity', PythonActivity.mActivity)

    def on_activity_result(request_code, result_code, intent):
        if request_code != RESULT_LOAD_IMAGE:
            Logger.warning('select_media: ignoring activity result that was not RESULT_LOAD_IMAGE')
            return

        if result_code == Activity.RESULT_CANCELED:
            Clock.schedule_once(lambda dt: callback(None), 0)
            return

        if result_code != Activity.RESULT_OK:
            raise NotImplementedError('Unknown result_code "{}"'.format(result_code))
        self.multi_imgs = []

        mClipData = intent.getClipData();
        list0 = [];
        for i in range(0, mClipData.getItemCount()):
            item = mClipData.getItemAt(i);
            uri = item.getUri();
            list0.append(uri);
            filePathColumn = [MediaStore_Images_Media_DATA];
            cursor = currentActivity.getContentResolver().query(uri,
                    filePathColumn, None, None, None);
            cursor.moveToFirst();
            columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            itemPath  = cursor.getString(columnIndex);
            self.multi_imgs.append(itemPath);
            cursor.close();
            Clock.schedule_once(lambda dt: callback(itemPath), 0)
        self.selected_images = self.multi_imgs

    activity.bind(on_activity_result=on_activity_result)
    intent = Intent()

    intent.setAction(Intent.ACTION_GET_CONTENT)
 #   intent.setData(Uri.parse('content://media/internal/images/media'))
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, True)
    intent.setType(type0)
    currentActivity.startActivityForResult(intent, RESULT_LOAD_IMAGE)
  #  chooser = Intent.createChooser(intent,String("Select Picture"))
  #  currentActivity.startActivityForResult(chooser, RESULT_LOAD_IMAGE)
Answers: