Android Question save and open external files in 26+

John W. Miescher

Member
Licensed User
To be compatible with android:targetSdkVersion="26" I tried to create and then download, save and then open/view files of various types. I can do so using rp As RuntimePermissions (thanks to Erels tutorial), but not yet in an external directory (to survive app uninstalls).

Is there another tutorial or example app from which an amateur like me could learn how to do it as I am still struggling with fileProvider and intents to open/show some files (e.g. PDF in Acrobat Reader).

Wouldn't it be nice to have these features already built in B4A?

john m.
 

DonManfred

Expert
Licensed User
The Tutorials are all just there...
Start with
https://www.b4x.com/android/forum/t...etsdkversion-minsdkversion.87610/#post-554210

Fileprovider:
- 24 - must use FileProvider when sharing files (https://www.b4x.com/android/forum/t...om-your-app-with-file-provider.70458/#content)

Examples from the Fileprovider Tutorial.

- Example of using FileProvider to share an image with a third party viewer: https://www.b4x.com/android/forum/t...ut-fileuriexposedexception.72538/#post-461501
- Example of sharing a file (any file): https://www.b4x.com/android/forum/threads/share-txt-files.73166/#post-464921
- Receiving shared images from other apps: https://www.b4x.com/android/forum/threads/receiving-shared-images-from-other-apps.81041/#post-513532

Maybe you are interested in accessing a USB Storage
https://www.b4x.com/android/forum/threads/externalstorage-access-sd-cards-and-usb-sticks.90238/
 
Last edited:

John W. Miescher

Member
Licensed User
Thanks Don, that was extremely fast.
Yes, I've seen all except the last two and when I followed the instructions in the second one and added the filePicker library I got this: Error parsing manifest script: Module FilePicker not found (Manifest Editor). Why?

In fact I was hoping to find all answers in a single example.

john m.
 

Erel

Administrator
Staff member
Licensed User

John W. Miescher

Member
Licensed User
DonManfred has already provided you with all the information you need. You do however need to read it and understand.

Example of sharing files: https://www.b4x.com/android/forum/threads/share-txt-files.73166/#post-464921
I think that this is what you are trying to implement.

Note that there is no filePicker library.
Thanks Erel,
I did read it all and downloaded all the samples, it's the understanding that hurts!

All examples use files in File.DirAssets, but I still don't know how to create a off-app directory and save downloaded files to it.
Before I just created it with RootPath = File.DirRootExternal&"/myStuff.dir", followed by File.makedir... How does it work with 26+ ?
john m.
 

Erel

Administrator
Staff member
Licensed User
There are two different things here. If you just need to write or read files for your app then use File.DirInternal or RuntimePermissions.GetSafeDirDefaultExternal (I would choose File.DirInternal).

If you want to share a file with other apps then save it in RuntimePermissions.GetSafeDirDefaultExternal("shared") and share it as demonstrated in this example: https://www.b4x.com/android/forum/threads/share-txt-files.73166/#post-464921

All examples use files in File.DirAssets
That's not true.
The very simple example just copies a file from DirAssets to the shared folder:
B4X:
File.Copy(File.DirAssets, "1.bal", Starter.shared, "1.bal")
You can remove this line and create the file in any way you like.
 

John W. Miescher

Member
Licensed User
I am obviously too dumb for this, perhaps some else can tell me how to create a new directory outside my app (e.g. Internal storage/Android/data/B4A.example plus sub-directories) and then save downloaded files to it.
 

John W. Miescher

Member
Licensed User
Yes.

My users can
  1. download and locally save any type of digital assets
  2. this will add the item to an alphabetical list
  3. they can also add other locally stored material from any source to that list
  4. they can click on an item in that list and open, read, view or listen to it
  • to preserve the db and the downloaded material beyond app updates the downloaded items must be stored in a folder not dependent on my app.
  • to open or view any item it would be copied to the share folder (if necessary) and the required process launched with an intent.
 

Erel

Administrator
Staff member
Licensed User
Permission to write:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim rp As RuntimePermissions
   rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
   Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
   If Result Then
       Log("You can write wherever you like")
       File.WriteString(File.DirRootExternal, "1.txt", "aaa")
   Else
       Log("you can only write in File.DirInternal or rp.GetSafeDirDefaultExternal")
   End If
End Sub
to open or view any item it would be copied to the share folder (if necessary) and the required process launched with an intent.
You need to follow the example that was posted 8 times in this thread.

In that example the file is shared from rp.GetSafeDirDefaultExternal("shared'). This is declared in the manifest editor:
<external-files-path name="name" path="shared" />.

If you want to share from the root directly then change it to:
<external-path name="name" path="" />

If you have any specific question then please start a new thread.
 

John W. Miescher

Member
Licensed User
This just proves that I was right in saying "I am obviously too dumb for this".

Thanks a lot, I'll try it again. I had the CheckAndRequest bit earlier in starter.bas which did nothing and I did not know about root dir vs. shared in manifest.
 

John W. Miescher

Member
Licensed User
That was actually the first thing I did, but obviously was too dumb to catch and retain some details.
Sorry to have caused you all this trouble.
john m.
 
Top