Java, Codename One: Exporting and -most importantly- importing files between users

115 Views Asked by At

Here the problem: my app will generate some files, and I want to give to the users the opportunity to exchange these files between them.

This requires 3 steps:

  1. Saving the data: easily done in Storage implementing the functions required by the Externalizable class;
  2. Sharing the data: done (probably, right now it's impossible to check if the result is correct because the missing step 3) with the sharing methods offered by the framework, as soon as I understood I needed to use as mimetype "application/octect-stream";
  3. importing the downloaded data (shared by another user): this one I can't manage to find a way to make it work. Loading the files from the app's Storage is easy, but accessing to the folders out of the app's Storage is something I can't manage to do.

I used FileSystemStorage in the hope of gaining access at least to the Download folder that (mostly) every phone has, but apparently I can't manage to accomplish the task

Using the FileSystemStorage on Android, for example, I have access to

  • /storage/emulated/0
  • /storage/emulated/legacy
  • file:///system

The first two being related to the Storage of the app.

Acceding to file:/// I obtain a long list of folders, a partial list including

  • media
  • logs
  • sdcard
  • Removable
  • ...
  • root
  • ...

But when I try to access some of these, they all appear to be empty. Either I make some mistake or the app can't see their content.

So I wonder if there is a way to accomplish the task, namely to have access to the files (probably in the Download folder) the user has downloaded, to import them.

2

There are 2 best solutions below

2
On

Phone apps live in isolation within the phone. Android literally creates a separate Linux user for every app so they don't step on each other and damage the phone. iOS does similar tricks.

As a result apps can't just write a file to downloads and can't just list all the files there. This would violate the users privacy. So the native API to share a file is usually separate from the files API. We do have a share API in the CN class which lets you share images etc. to an arbitrary OS app. See isNativeShareSupported and share.

0
On

Ok, maybe I found a solution for reading the files from the Download folder in an extension of CodenameOne called FileChooser.

According to this blog post it should give access to, between the others, the Download folder (at least in Android).

I'm going to try it and, when everything is ready and tested, edit this reply to say how it worked out for me.