Android Question Where https downloads go and how to access them on device using a device's File Manager?

johnaaronrose

Active Member
Licensed User
Longtime User
Currently I am downloading a file with https using Erel's post #2 at https://www.b4x.com/android/forum/threads/solved-download-files-from-server-with-progress.126797/.
First question:
where does the download go, in terms of the device's file structure?

I noticed some coding on copying a file to File.DirInternal/"filename.dat" at https://www.b4x.com/android/forum/threads/b4x-okhttputils2-with-wait-for.79345/#content:
B4X:
Sub DownloadAndSaveFile (Link As String)
   Dim j As HttpJob
   j.Initialize("", Me)
   j.Download(Link)
   Wait For (j) JobDone(j As HttpJob)
   If j.Success Then
       Dim out As OutputStream = File.OpenOutput(File.DirInternal, "filename.dat", False)
     File.Copy2(j.GetInputStream, out)
     out.Close '<------ very important
   End If
   j.Release
End Sub

Second question:
If I wanted to write the downloaded file to the standard Downloads directory so that it would be accessible by me using the device's File Manager, what would I write instead of File.DirInternal, "filename.dat") in the line starting with "Dim out"?
 

teddybear

Well-Known Member
Licensed User
where does the download go, in terms of the device's file structure?


Second question:
If I wanted to write the downloaded file to the standard Downloads directory so that it would be accessible by me using the device's File Manager, what would I write instead of File.DirInternal, "filename.dat") in the line starting with "Dim out"?
1. The j is httpjob object, when j.download is completed, you can get bitmap, inputstream and string ... no device's file structure.
2. You can see this
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
the download is in httpjob´s temp-folder.
As written by @teddybear you can get a Inputstream in JobDone when the download is Successful.
Use the Importstream and copy the data to whereever you want.

B4X:
   Dim j As HttpJob
   j.Initialize("", Me)
   j.Download(Link)
   Wait For (j) JobDone(j As HttpJob)
   If j.Success Then
       Dim out As OutputStream = File.OpenOutput(File.DirInternal, "filename.dat", False)
     File.Copy2(j.GetInputStream, out)
     out.Close '<------ very important
   End If
   j.Release
 
Upvote 0

johnaaronrose

Active Member
Licensed User
Longtime User
the download is in httpjob´s temp-folder.
As written by @teddybear you can get a Inputstream in JobDone when the download is Successful.
Use the Importstream and copy the data to whereever you want.

B4X:
   Dim j As HttpJob
   j.Initialize("", Me)
   j.Download(Link)
   Wait For (j) JobDone(j As HttpJob)
   If j.Success Then
       Dim out As OutputStream = File.OpenOutput(File.DirInternal, "filename.dat", False)
     File.Copy2(j.GetInputStream, out)
     out.Close '<------ very important
   End If
   j.Release
Paraphrasing the second question in my previous post #1:
I want to write the downloaded file to a directory so that it would be accessible by me using the device's File Manager. What directory would you recommend i.e. what would I write instead of File.DirInternal, "filename.dat") in line 6 starting with "Dim out"?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Check the link in teddybear 2nd point.

You can also use the following class.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
What directory would you recommend i.e. what would I write instead of File.DirInternal, "filename.dat") in line 6 starting with "Dim out"?

Just to be absolutely clear, Android has, for some time now, been progressively restricting the access that an app has to general device storage. The ability to access general device storage from an app using simple FileWrite routines and folder and file names has long gone, so there is nothing that will work in Line 6. Effectively only File.DirInternal remains as a convenient destination for File.Write, and that uses storage that is inaccessible outside of your app.

Most of what one might call "general device storage" is out of reach to us, although some system folders can be reached using somewhat complicated paths - that is what is being described in the responses above.

"But how do File Manager apps work?" you will no doubt be thinking. They work because they have negotiated with the Google PlayStore to obtain additional permissions - not something that I can describe well. Even they cannot transfer files in and out of your app's File.DirInternal.
 
Upvote 0
Top