2

I'm coding an Android app using Kivy, and would like the app to write to the android filesystem, some PUBLIC directory/files.

Currently, if my app writes a file (using Python), the file CAN be read by the android local File Manager, but CANNOT be read by my computer mtp (these files don't appear in the InternalStorage). The adb shell CAN see the files though.

[EDIT] The solution could be to scan the files for mtp using MediaScannerConnection with Pyjnius, however as shown in my next answer, it didn't work for me. [/EDIT]

It seems to me that I'm looking for an equivalent of the Java getSharedPreferences described here : http://developer.android.com/guide/topics/security/permissions.html.

I thought that there is a similar option in the buildozer spec : the android.private_storage field described here : https://raw.githubusercontent.com/kivy/buildozer/master/buildozer/default.spec.

However, I'm not getting this to work... maybe I'm not using correctly the buildozer command : after settings in the spec "android.private_storage = False", I tried several commands like "buildozer android release --dir public my_dir" , or "buildozer android release --public my_dir", etc... , without success.

MoriB
  • 311
  • 1
  • 2
  • 10
  • More or less SOLVED : this is apparently a known bug in mtp : some files, depending on how they are written (I'm using Python "open" instruction) don't show until you REBOOT the device. Indeed, rebooting the device did help. I'm still not sure how to use the buildozer private_storage option though. – MoriB Aug 16 '14 at 09:42
  • 1
    The private storage thing is just a boolean toggle for where the app data should be stored - if True, it goes in /data/data/... like a normal app. If False, the python stuff is put in the external storage directory so you can edit/access it more easily - the former is normal and the default. – inclement Aug 16 '14 at 10:36
  • Interesting, thanks inclement – MoriB Aug 16 '14 at 13:40
  • MoriB do post your comment as an answer and accept it. It's considered good practice at stackoverflow – ndemou Sep 08 '14 at 12:39

2 Answers2

2

I've been trying without success to make the files viewable without reset, using MediaScannerConnection in Pyjnius. After having written the files in the file system, the goal is to scan them. Here is the code that runs but doesn't have any apparent effect.

from jnius import autoclass , cast
from jnius import PythonJavaClass, java_method

PythonActivity = autoclass('org.renpy.android.PythonActivity')
activity = cast('android.app.Activity', PythonActivity.mActivity)
context = activity.getApplicationContext()

MediaScannerConnection = autoclass('android.media.MediaScannerConnection')
MediaScannerConnection.scanFile(context,successFiles,None,None)   #successFiles is a list of absolute paths of files on Android

I've been trying also to use the other overloaded form of the scanFile method, but I can't define correcty the needed interface :

class OnScanCompletedListener(PythonJavaClass):
    __javainterfaces__ = ['android.media.MediaScannerConnection$OnScanCompletedListener']


    @java_method('(Ljava.lang.String;Landroid.net.Uri;)V')
    def onScanCompleted(self, path, uri):
        pass

    @java_method('()V')
    def onMediaScannerConnected(self):
        pass


MediaScannerConnection = autoclass('android.media.MediaScannerConnection')
mediaScannerConnectionListener = OnScanCompletedListener()
mScanner = MediaScannerConnection(context,mediaScannerConnectionListener)
mScanner.connect()
for thefile in successFiles:
    mScanner.scanFile(thefile,None) 
MoriB
  • 311
  • 1
  • 2
  • 10
1

More or less SOLVED : this is apparently a known bug in mtp : some files, depending on how they are written (I'm using Python "open" instruction) don't show until you REBOOT the device. Indeed, rebooting the device did help.

MoriB
  • 311
  • 1
  • 2
  • 10
  • 1
    It's not a bug but feature: The MTP database needs to be refreshed with media scanner. Booting the device runs the media scanner so that's why it helps. In Android Java you'd use `MediaScannerConnection` to refresh the database; don't know how to do it in Kivy. – laalto Sep 09 '14 at 13:20
  • Thanks laalto. I guess you mean calling the scanFile method on the new files. I'll try to call it with Pyjnius from Kivy. – MoriB Sep 09 '14 at 13:27