MoriB
MoriB

Reputation: 311

kivy android : write public files

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.

Upvotes: 2

Views: 2240

Answers (2)

MoriB
MoriB

Reputation: 311

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) 

Upvotes: 2

MoriB
MoriB

Reputation: 311

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.

Upvotes: 1

Related Questions