Android Question Share txt files

Discussion in 'Android Questions' started by Sergio GermánRassino, Nov 17, 2016.

  1. Sergio GermánRassino

    Sergio GermánRassino Member Licensed User

    Hello. Thank you for your help for new users.
    I need to share a txt file by bluetooth and other aps in my phone, and I been reading all about it and couldn't find a solution.

    I found this that opens many apps to share with, in my phone, but not the bluetooth option.

    Dim share As MESShareLibrary
    share.sharebinary("file://" & File.DirRootExternal & "/SipAsistencia/Asistencia/"&Main.cursoelegido&"_"&Main.day&Main.month&Main.year&".txt", "text/txt", "Share the love!", "This is the text to go in the body!")

    This other code seems to be more apropiate, and shows bluetooth and all other apps to share, but it sends an html file with the txt file name included, instead of the txt file

    Dim share As Intent
    Dim archivo As String = "file://" & File.DirRootExternal &"/SipAsistencia/Asistencia/1A2016_16112016.txt"
    share.Initialize(share.ACTION_SEND,"")
    share.SetType("text/plain")
    share.PutExtra("android.intent.extra.TEXT", archivo)
    share.WrapAsIntentChooser("Share text via")
    StartActivity(share)

    Could someone help me with this?
     
    Last edited: Nov 17, 2016
  2. Erel

    Erel Administrator Staff Member Licensed User

    Please use [code]code here...[/code] tags when posting code.

    Do you want to share the text from the text file or share it as a file?
     
  3. Sergio GermánRassino

    Sergio GermánRassino Member Licensed User

    Thanks Erel for your reply.
    I need to share it as a txt file, for example: 1A2016_16112016.txt
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Things have changed with Android 7. You need to use FileProvider.

    Example of sharing a file is attached.

    Make sure to copy the manifest editor code to your project.

    Note that the best solution is to only use FileProvider when Phone.SdkVersion >= 24. Use the "file://" uri on older devices.

    Edit: better solution: https://www.b4x.com/android/forum/threads/97865/#content
     
    Last edited: Oct 10, 2018
    Powie, koaunglay, pesquera and 6 others like this.
  5. Sergio GermánRassino

    Sergio GermánRassino Member Licensed User

    Thank you Erel. Worked perfect!
    Only a small detail: Your example sends the file by bluetooth directly. Is there any chance to show all apps to sheare with , and then user can choice Bluetooth (or other app) to share with?
    It would be a grate alternative for users with no bluetooth device in their pc.
    Thank you for your help
    Sergio
     
  6. DonManfred

    DonManfred Expert Licensed User

    No. I got a question to share with which app and alist of apps to choose from... I choosed dropbox.
    The 1.bal was sent to my dropbox...
     
    MarcoRome likes this.
  7. Erel

    Erel Administrator Staff Member Licensed User

    This means that you have selected Bluetooth in the past with the Use Always option.

    You can reset it: http://forums.androidcentral.com/google-nexus-7-tablet-2012/223129-resetting-just-once-always.html
    Or you can force the dialog to show all options with:
    Code:
    in.WrapAsIntentChooser("Select")
     
    yiankos1 and DonManfred like this.
  8. Sergio GermánRassino

    Sergio GermánRassino Member Licensed User

    Yes! I restored both tablet an phone preferences and everything begun to work fine.
    Thank you DonManfred and Erel. It is very important to have your help
    Prof Sergio Rassino
     
  9. ilan

    ilan Expert Licensed User

    hi i am trying this example "as is" but i am getting a crash:

    what could be the reason? thank you
     
  10. ilan

    ilan Expert Licensed User

    ok this code seems to do the job without putting anything in manifest

    Code:
    Dim u As Uri 'ContentResolver library
    u.Parse("file://" & File.Combine(File.DirRootExternal, "07-2017.xls"))
    Dim i As Intent
    i.Initialize(i.ACTION_SEND, 
    "")
    i.SetType(
    "*/*")
    i.PutExtra(
    "android.intent.extra.STREAM",u)
    i.WrapAsIntentChooser(
    "Select")
    StartActivity(i)
    i am able to share it via whatsapp and email. is it also possible to add a text to the msg?

    EDIT: ok this code will do it but it will limit the sharing only to email type apps

    Code:
    Dim subjectstr As String = "my subject"
        
    Dim msgstr As String = "my msg"
        
    Dim u As Uri 'ContentResolver library
        u.Parse("file://" & File.Combine(File.DirRootExternal, "file.xls"))
        
    Dim int1 As Intent
        int1.Initialize(int1.ACTION_SEND, 
    "")
        int1.PutExtra(
    "android.intent.extra.SUBJECT",subjectstr)
        int1.putExtra(
    "android.intent.extra.TEXT",msgstr)
        int1.PutExtra(
    "android.intent.extra.STREAM",u)
        int1.SetType(
    "message/rfc822")
        int1.WrapAsIntentChooser(
    "Select"'will show always ContentChooser
        StartActivity(int1)
     
    Last edited: Aug 4, 2017
  11. Erel

    Erel Administrator Staff Member Licensed User

    Example in post #4 updated. FileProvider is only used in Android 7+.
     
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