Saving a single private binary file to internal storage on Android

742 views
Skip to first unread message

Zach Dunlap

unread,
Oct 13, 2014, 4:08:07 PM10/13/14
to kivy-...@googlegroups.com
I'm using requests to get a file from a REST server and save it. I can do this no problem on the desktop but can't figure out how to make it work on Android.

I doing the following in python:
with open('FILENAME', 'wb') as tempfile:
            download = requests.get(url)
            dcontent = download.content
            tempfile.write(dcontent)
            tempfile.close

Is this something I can do with Kivy on android or would I need to approach this differently? 

Zach Dunlap

unread,
Oct 14, 2014, 9:39:45 AM10/14/14
to kivy-...@googlegroups.com
I solved my own issue (although I'm not sure it's the best solution) so I thought I would share in case anyone else has the same problem.

For my app I created a folder called "temp" and inside it, I created an empty file with the same name & extension I will save it as later.

I then made sure I updated the buildozer.spec file and added the file's extension to the 'source.include' section (in this case I added mp3 to that section).

When my app runs and needs to save the file now, I can point it at that file and save to the temp folder I created and it works well.

I'm going to guess there are better or more proper ways of handling this, but for now it's a start and gets me one step further.

knappador

unread,
Oct 14, 2014, 9:47:18 AM10/14/14
to kivy-...@googlegroups.com
Writing to the app's private directory "just works" using os.abspath('./foo') etc.  When I need to share data with other apps, I'll use the Environment methods to get the appropriate folder.  Here's some code I used recently that just shows you a variety of things that also worked.

    def get_save_root(self):
        if platform == 'android':
            from jnius import autoclass
            Environment = autoclass('android.os.Environment')
            if Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED:
                #root = Environment.getExternalStorageDirectory().getAbsolutePath()
                root = Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath() + '/'
            else:
                #root = context.getExternalFilesDir().getAbsolutePath()
                root = os.getcwd() + '/tmp/'
        else:
            root = os.getcwd() + '/tmp/'
        #root += '/knapps_apps.reduce/'
        self.save_root = root 
        return root

I'm sure you can infer that I employed this code here:

And also that I would appreciate a download and good rating since I'm currently trying to get it into search rankings so that it can find its own way ^_^

Zach Dunlap

unread,
Oct 14, 2014, 9:52:26 AM10/14/14
to kivy-...@googlegroups.com
Awesome thanks! I'll definitely check your app out :-).

Lord Xelous

unread,
Sep 2, 2015, 7:28:51 AM9/2/15
to Kivy users support
Sorry to resurrect this post after to long, but I have this exact problem going on now with Android API 21.

I've gotten my Kivy App built and on the device, whenever I make the "open" call from python I see the file create, but simply can't write to that file.

I've tried the code pasted in from knappador however, it simply does not work for me, I've also tried to create a "temp.tmp" file in the project and include it in the build spec,
but as I'm using the private storage I can't see that included file...

Soooo, I was wondering, have either of you got a start-to-end example you could knock together and share?... It seems a simple problem, but one which is not addressable from python directly atm.


Zach Dunlap

unread,
Sep 2, 2015, 9:25:08 AM9/2/15
to Kivy users support
I've only overwritten a file and never got a chance to try knappador's solution. 

Are you sure you've updated the buildozer spec to include the file type and all? The solution to my own problem was more of a workaround and I'm pretty sure knappador's solution is the best way, but without trying it I am not sure. Perhaps knappador will get a chance to provide some more input.
Reply all
Reply to author
Forward
0 new messages