B4A Library [class] FileProvider - share files

Discussion in 'Additional libraries, classes and official updates' started by Erel, Oct 3, 2018.

Thread Status:
Not open for further replies.
  1. Erel

    Erel Administrator Staff Member Licensed User

    After investigating several issues with the current FileProvider code that you can find in the forum, I decided to make some improvements and implement it in a class.

    Starting from Android 7 (API 24) you cannot directly share file uris with other applications. You need to use FileProvider.

    The FileProvider class should work on all Android versions (4+).

    Instructions:

    1. Add FileProvider class to your project. It depends on: RuntimePermissions, JavaObject and Phone libraries. It doesn't require any permission.

    2. Add to the manifest editor:
    Code:
    AddManifestText(<uses-permission
       android:name=
    "android.permission.WRITE_EXTERNAL_STORAGE"
       android:maxSdkVersion=
    "18" />
    )

    AddApplicationText(
      <provider
      android:name=
    "android.support.v4.content.FileProvider"
      android:authorities=
    "$PACKAGE$.provider"
      android:exported=
    "false"
      android:grantUriPermissions=
    "true">
      <meta-data
      android:name=
    "android.support.FILE_PROVIDER_PATHS"
      android:resource=
    "@xml/provider_paths"/>
      </provider>
    )
    CreateResource(xml, provider_paths,
       <files-
    path name="name" path="shared" />
    )
    files-path = File.DirInternal

    3. Initialize FileProvider in Service_Create of the starter service.

    4. See attached example for the code required to create several types of intents.

    [​IMG]

    Current version: 1.00
     

    Attached Files:

  2. mddmx

    mddmx Member Licensed User

    Erel, THANK YOU VERY MUCH. This works great!
     
  3. incendio

    incendio Well-Known Member Licensed User

    Thanks, but there is still an issue with sending mail with attachement.

    I make an email body to the code
    Code:
    Sub btnSendEmail_Click
       
    Dim FileName As String = "b4a.png"
       
    'copy the shared file to the shared folder
       File.Copy(File.DirAssets, FileName, Starter.Provider.SharedFolder, FileName)
       
    Dim email As Email
       
    email.To.Add("aaa@bbb.com")
       
    email.Subject = "subject"
       
    email.Body = "This is a test" 'This is the added code
       email.Attachments.Add(Starter.Provider.GetFileUri(FileName))
       
    email.Attachments.Add(Starter.Provider.GetFileUri(FileName)) 'second attachment
       Dim in As Intent = email.GetIntent
       
    in.Flags = 1 'FLAG_GRANT_READ_URI_PERMISSION
       StartActivity(in)
    End Sub
    On my email (gmail), an error message pop up, Unable to attach file.
     
    Last edited: Oct 4, 2018
  4. Erel

    Erel Administrator Staff Member Licensed User

    Are you running the example as-is?
     
  5. incendio

    incendio Well-Known Member Licensed User

    Yes, except for added code for email body.

    If code for email body deleted, just like your example, the program runs OK.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    1. Which Android version?
    2. What happens when you click on the other buttons?
     
  7. incendio

    incendio Well-Known Member Licensed User

    1. Android oreo 8.0
    2. The others buttons were OK
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Does it work with a single attachment?
     
  9. scsjc

    scsjc Active Member Licensed User

    I test all, and work fine... really work great.
    Thanks !!!
     
  10. incendio

    incendio Well-Known Member Licensed User

    No, it didn't.
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    An email intent with a single attachment is the same intent as the share file intent. Lets wait for feedback from other users.
     
  12. incendio

    incendio Well-Known Member Licensed User

    There is something strange here.

    Here are the steps
    1. First clicked on Send Mail -> failed to load attachment
    2. Second clicked, without quit application -> worked fine
    3. Quit application then clicked again Send mail -> still worked fine
    4. Uninstall app, then install again, first clicked -> failed to load attachment, second clicked and so on -> worked fine

    I can reproduce the behavior by install and uninstall app.

    Any idea, why this is happen ?

    Another thing, on Lolipop, there is a shared folder created under DirInternal (i think), but on oreo, there wasn't such folder, where is shared folder created under Oreo?
     
    Last edited: Oct 4, 2018
  13. KMatle

    KMatle Expert Licensed User

    My apps are ok so far because I copy (file.copy...) the needed files to a folder my app has access to before I share anything or similar. So if the copy works, everything else works. Am I wrong here?
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    @incendio please start a new thread for further discussion. It doesn't look relevant to FileProvider.

    You must use FileProvider if you want to share any of these files with an intent.
     
  15. incendio

    incendio Well-Known Member Licensed User

  16. GuyBooth

    GuyBooth Active Member Licensed User

    How does this relate to the ExternalStorage Class (which contains considerably more code)?

    Is its purpose completely different?
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    They are not related.

    FileProvider allows you to share files with other applications using an intent.
    ExternalStorage allows your app to access sd cards and usb devices.
     
    mendiburen, anOparator and GuyBooth like this.
  18. GuyBooth

    GuyBooth Active Member Licensed User

    Ok, thanks for the clarification.
     
  19. FrankDev

    FrankDev Active Member Licensed User

    Hello,

    I have a problem with the test program.
    Image and text sharing are working fine.
    With 'Mail' there is a problem
    Android 8.1

    Dim in As Intent = email.GetIntent

    Code:
    --------- beginning of main
    Copying updated assets files (
    2)
    *** 
    Service (starter) Create ***
    Using FileProvider? 
    true
    ** 
    Service (starter) Start **
    ** 
    Activity (main) Create, isFirst = true **
    ** 
    Activity (main) Resume **
    Error occurred on line: 
    58 (Main)
    java.lang.ClassCastException: android.net.Uri$HierarchicalUri cannot be cast 
    to java.lang.String
     at anywheresoftware.b4a.phone.Phone$
    Email.getIntent(Phone.java:677)
     at anywheresoftware.b4a.phone.Phone$
    Email.GetIntent(Phone.java:694)
     at b4a.examplewewe.main._btnsendemail_click(main.java:
    430)
     at java.lang.reflect.Method.invoke(Native Method)
     at anywheresoftware.b4a.shell.Shell.runMethod(
    Shell.java:710)
     at anywheresoftware.b4a.shell.Shell.raiseEventImpl(
    Shell.java:339)
     at anywheresoftware.b4a.shell.Shell.raiseEvent(
    Shell.java:249)
     at java.lang.reflect.Method.invoke(Native Method)
     at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:
    139)
     at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    170)
     at anywheresoftware.b4a.BA.raiseEvent(BA.java:
    166)
     at anywheresoftware.b4a.objects.ViewWrapper$
    1.onClick(ViewWrapper.java:80)
     at android.view.View.performClick(
    View.java:6304)
     at android.view.View$PerformClick.run(
    View.java:24803)
     at android.os.Handler.handleCallback(Handler.java:
    790)
     at android.os.Handler.dispatchMessage(Handler.java:
    99)
     at android.os.Looper.loop(Looper.java:
    171)
     at android.app.ActivityThread.main(ActivityThread.java:
    6633)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:
    547)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    823)
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    You need to use a newer version of Phone library. Current version is 2.50.
     
Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice