Android Question Download a file from my app to user phone? (SOLVED)

Stichler

Active Member
Licensed User
I may be very bad at this so I appreciate your patience, but for the life of me I can not figure out how to have a file in my app, in this case a .pdf, and let the user download the file to their phone via a button click. Help please?
 

Stichler

Active Member
Licensed User
As of now in the assets folder. I want it to be part of the app, then the user can click the button and download it directly from the qpp
 
Upvote 0

Stichler

Active Member
Licensed User
It is a coloring page, im unsure where to save it on their phone? I tried clicking to just open it so they could print it out but cant figure out the code to do so.
 
Upvote 0

Stichler

Active Member
Licensed User
I tried this:

B4X:
Dim i As Intent
    i.Initialize(i.ACTION_VIEW, "file://" & File.DirAssets & "/christmastree.pdf")
    i.SetType("application/pdf")
    StartActivity(i)

but get this:

** Service (starter) Destroy (ignored)**
** Activity (coloring) Pause, UserClosed = true **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
** Activity (coloring) Create, isFirst = true **
** Activity (coloring) Resume **
Error occurred on line: 38 (Coloring)
android.os.FileUriExposedException: file://AssetsDir/christmastree.pdf exposed beyond app through Intent.getData()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:1958)
at android.net.Uri.checkFileUriExposed(Uri.java:2356)
at android.content.Intent.prepareToLeaveProcess(Intent.java:10511)
at android.content.Intent.prepareToLeaveProcess(Intent.java:10465)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1616)
at android.app.Activity.startActivityForResult(Activity.java:4564)
at android.app.Activity.startActivityForResult(Activity.java:4522)
at android.app.Activity.startActivity(Activity.java:4883)
at android.app.Activity.startActivity(Activity.java:4851)
at anywheresoftware.b4a.keywords.Common.StartActivity(Common.java:848)
at b4a.example.coloring._btntree_click(coloring.java:395)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:735)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:357)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:260)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6891)
at android.widget.TextView.performClick(TextView.java:12651)
at android.view.View$PerformClick.run(View.java:26083)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
 
Upvote 0

npsonic

Active Member
Licensed User
If your app always needs that specific file why not just add it to File dir so you can access it from path File.Combine(File.DirAssets,"yourfilename.pdf")
 
Last edited:
Upvote 0

Stichler

Active Member
Licensed User
Its in my app assests, but its a coloring page that i want the user to be able to ooen and print, i think i figured it out
 
Upvote 0

npsonic

Active Member
Licensed User
Its in my app assests, but its a coloring page that i want the user to be able to ooen and print, i think i figured it out
You can copy the file anywhere you want from assets.
 
Upvote 0

Stichler

Active Member
Licensed User
Ok so what would i code to click a button and have the pdf file saved to their phone? Im sorry im so bad at this, ty for your patience.
 
Upvote 0

npsonic

Active Member
Licensed User
Ok so what would i code to click a button and have the pdf file saved to their phone? Im sorry im so bad at this, ty for your patience.
You can design it anyway you like. File can be copied for example to external dir after you have get permission from user.

You can add button or it can be automatic copy after your app has permission to write external dir.
 
Upvote 0

npsonic

Active Member
Licensed User
Im sorry, im asking for a code example.
Check more about RuntimePermissions. If your app don't have write permission this does not work.
B4X:
File.Copy(File.DirAssets,"yourfilename.pdf",File.DirRootExternal,"yourfilename.pdf")
 
Upvote 0

Stichler

Active Member
Licensed User
I use this code, but nothing is being copied to download folder. I have permissions set properly I check in log list permissions.

B4X:
 File.Copy(File.DirAssets,"christmastree.pdf",File.DirDefaultExternal,"Download")
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…