How can I share files in my app's private data directory?

Thanks very much for all your suggestions. However I have still not succeeded.

The blocks I've been using for my experiments are along these lines (the app name is FE3):

TwoBlocksFromFE3

If I put test.txt in TextBox1_SaveFileName, what should I put in TextBox2_ShareFileName?

(If the filename in TextBox1 is prefaced by / (/test.txt) then there is no problem: /sdcard/test.txt in TextBox2 works and I can share the file. But apparently (http://appinventor.mit.edu/blogs/evan/2020/08/08/file-path-updates-android-10) this will no longer be allowed in later versions of Android.)

The experiments described below are done on Android 8.1.0 but I think my experiments with Android 10 were similar.

I've tried putting the following in TextBox2:

test.txt

/storage/emulated/0/Android/data/appinventor.ai_mickofemsworth.FE3/files/test.txt
(This is based on Anke's ASD extension.)

file:///emulated/0/Android/data/appinventor.ai_mickofemsworth.FE3/files/test.txt

These all resulted in file not found errors.

This is based on the assumption that the ASD is the same as the private directory. Taifun says this is not so, so I found a path for the private directory somewhere (one of Anke's posts?):
/data/user/0/appinventor.ai_mickofemsworth.FE3/files/test.txt
which resulted in a different error: "failed to find configured root that contains .../data/data/app...

I also tried putting
storage/emulated/0/Android/data/appinventor.ai_mickofemsworth.FE3/files/test.txt
in TextBox1_SaveFile - this produced a file could not be created error.

Any ideas? Thanks!

it seems you didn't read this discussion

I'm slightly confused about which discussion you are referring to. One of your posts is about saving a file called /test.txt or /a.txt. This isn't a problem because I can share using /sdcard/test.txt. The problem is when there's no / so the file is stored in the app's private directory.

you might want to read starting from here again

Taifun

@Michael, See post 11 and 13, you have to save in ASD, not in private dir

I've tried saving to the ASD with this block:
AsdSaveBlock
but this does not work. I'm not sure what you (Taifun) mean: I only found three blocks in Anke's extension. I tried running the create ASD directory block before the SaveFile block but that still led to a could not create file error.

I am BTW @Michael_Wood.

you have to remove /storage/emulated/0 from the result provided by the AppSpecificDirectory method to get a relative path as explained by @Anke here

I agree it is a bit "complicated"...

it helps to be familiar with Do it... , see also tip 4 here App Inventor: How to Learn | Pura Vida Apps
see also Live Development, Testing, and Debugging Tools

Taifun

I have just tried this by putting t1 in TextBox1_SaveFile in the app with the blocks in post 20 above, and
/Android/data/appinventor.ai_mickofemsworth.FE3/files/t1
in TextBox2_ShareFile.
Have I got this right? I'm afraid it doesn't work.
(I'm not using the AI Companion and Do it because this just adds another layer of complication, and by putting suggestions in textboxes I can easily check if something works.)
Thanks for your help!

When saving you don't need extension you can use /filename, you need it only when sharing

Try this APK:

It should work on all Android versions.

1 Like

without seeing your blocks how can anyone help?

you are currently using the community as debugger... and you started asking on Oct 22, now is Nov 6...
using the companion app and Do it would help you to find out what you are doing much faster (in minutes not in days)

Taifun

3 Likes

Many thanks Anke. The apk seems to work as I can share the file and it is not saved in the root directory. I initially thought that as the file saved is /filename.txt (with the /) it would be saved in the root directory which will not be allowed in later versions of Android. But I'm obviously wrong. I will use your blocks to experiment when I've got more time tomorrow. Thanks again!

I really thought I'd tried the suggested path for saving in pre-10 versions of Android, but I obviously made a mistake because it works today. I think I've got the hang of it now. I'll post a summary of the answer to my original question in the next day or so.

In the meantime I've got just one further question - where do I find the ApiLevel block?

Thanks again.

you need this extension

Please don't post direct download links to extensions, give a link to the extension's topic instead:

2 Likes

So the answer to my question is that I can't share files in the app's private directory, but I can share them from the app specific directory. To do this I need to save a file as
/filename.txt in Android 10
and as
/Android/data/appinventor.ai_mickofemsworth.FE4/files/filename.txt in earlier versions of Android.
In both cases the file path for sharing is
/storage/emulated/0/Android/data/appinventor.ai_mickofemsworth.FE4/files/filename.txt
(The app name is FE4 and my username is mickofemsworth.)

I wonder why I didn't think of this! :grinning:

My version of Anke's blocks is:


This seems to work well on Android 10 and 8.10.

Many thanks for all your help.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.