Android Question Share txt files

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:

Erel

B4X founder
Staff member
Licensed User
Longtime 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:
Upvote 0

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
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Your example sends the file by bluetooth directly.
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...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
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?
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:
B4X:
in.WrapAsIntentChooser("Select")
 
Upvote 0

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
 
Upvote 0

ilan

Expert
Licensed User
Longtime 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.

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

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 50 (Main)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
at b4a.example.main._createfileprovideruri(main.java:458)
at b4a.example.main._button1_click(main.java:418)
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:78)
at android.view.View.performClick(View.java:6261)
at android.widget.TextView.performClick(TextView.java:11180)
at android.view.View$PerformClick.run(View.java:23748)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/b4a.example/files/shared/1.bal
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:679)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:378)
... 23 more
** Activity (main) Pause, UserClosed = true **

what could be the reason? thank you
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
ok this code seems to do the job without putting anything in manifest

B4X:
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

B4X:
    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:
Upvote 0
Top