Android Question Download and open a file from a password protected folder?

tsteward

Well-Known Member
Licensed User
Longtime User
I am trying to create a routine to download a file and open it with the default app for file type
This is what I have created so far but I get an error "Forbidden You don't have permission......"

url is something like "www.mysite.com/dir1/dir2/myfile.pdf"

My attempt
B4X:
Sub DownloadFile(url As String)
    Dim job As HttpJob

    job.Initialize("DownloadJob", Me)
    job.Username = "xxxxx" ' Replace with your actual username
    job.Password = "*****" ' Replace with your actual password
    job.Download(url)
    Dim fname As String = url.SubString(url.LastIndexOf("/")+1)
    Log(fname)
    Wait For (job) JobDone(job As HttpJob)
    If job.Success Then
        ' Save the downloaded file
        Dim out As OutputStream = File.OpenOutput(xui.DefaultFolder, fname, False)
        File.Copy2(job.GetInputStream, out)
        out.Close
    Else
        Log("Error downloading file: " & job.ErrorMessage)
    End If
    job. Release
End Sub
 

emexes

Expert
Licensed User
I get an error "Forbidden You don't have permission......"

I ran your exact code (ok, with added Log calls) and ran it in Debug mode on an Android phone, to fetch a convenient password-protected file, and it worked no problem.

Does it say what you don't have permission to do?

I'm about to set up a similar password-protected file that I can make public, so that we're both singing from the same hymn book.
 
Upvote 0

emexes

Expert
Licensed User
how to open file with intent

I am getting as far as:

Log output:
after wait
true
67293
before open
/storage/emulated/0/Android/data/b4a.example/files
before copy
before close
before release
after release
Error occurred on line: 78 (Main)
android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/b4a.example/files/nevergiveup.jpg exposed beyond app through Intent.getData()
    at android.os.StrictMode.onFileUriExposed(StrictMode.java:2141)
    at android.net.Uri.checkFileUriExposed(Uri.java:2399)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:11171)

Apparently the fix is:

https://www.b4x.com/android/forum/t...sed-beyond-app-through-intent-getdata.116933/

Holy smokes!

That sounds like an after-dinner project to me.
 
Upvote 0

emexes

Expert
Licensed User
an after-dinner project

Far out. I am so close. The file is downloaded, its size is correct, I'm using FileProvider, including FileProvider.UseFileProvider = True but I couldn't get nevergiveup.jpg to display ("invalid media") so I tried with index.html and Intent.SetType("text/html") and that doesn't work either, but at least one of the apps is kind enough to come back with:

B4X:
Web page not available

The web page at:
content://b4a.example.provider/name/index.html
could not be loaded because:

net::ERR_FILE_NOT_FOUND

so I'm going to sleep on it, see if any epiphanies hit me overnight.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
B4X:
Web page not available
The web page at:
content://b4a.example.provider/name/index.html
could not be loaded because:
net::ERR_FILE_NOT_FOUND
1.The httpjob just got the index.html, not got nevergiveup.jpg.
2.You use intent ACTION_VIEW and URI, you need to provide android.intent.extra.STREAM for the receiver
B4X:
    Dim i As Intent
'    i.initialize(i.ACTION_VIEW, provider.GetFileUri(fname))
    i.Initialize(i.ACTION_VIEW, "")
    i.SetType("text/html")
    i.PutExtra("android.intent.extra.STREAM", provider.GetFileUri(fname))
    i.Flags = 1 '
    StartActivity(i)
 
Upvote 0

emexes

Expert
Licensed User
you need to provide android.intent.extra.STREAM for the receiver

Far out, I never would have guessed that. Is a definite improvement, in that it doesn't bomb out now. But it still doesn't show the file, although the last two Log() lines suggest that it's at least trying to show the file, but returns straight away to the "calling" B4A app.

Does it run at your end? No worries if you didn't test it, you've given me some threads to pull on, so thank you for that. First step is to try with the other two files, see if they fare any better. No, that's the second step; first step is find out wtf is Intent.Flags !!!

B4X:
Log("intent 0")
    Dim i As Intent
Log("intent 1")
    i.Initialize(i.ACTION_VIEW, "")
Log("intent 2")
    i.SetType("text/html")
Log("intent 3")
    i.PutExtra("android.intent.extra.STREAM", provider.GetFileUri(fname))
Log("intent 4")
    i.Flags = 1 '
Log("intent 5")
    StartActivity(i)
Log("intent 6")
Log output:
Logger connected to:  HMD Global Nokia C01 Plus
--------- beginning of system
--------- beginning of main
/data/user/0/b4a.example/files/shared
true
index.html
/data/user/0/b4a.example/files/shared/index.html
224
intent 0
intent 1
intent 2
intent 3
intent 4
intent 5
intent 6
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Modified code, it should be action send
B4X:
    Dim i As Intent
    i.Initialize(i.ACTION_SEND, "")
    i.SetType("text/html")
    i.PutExtra("android.intent.extra.STREAM", provider.GetFileUri(fname))
    i.Flags = 1 '
    StartActivity(i)

Here is my phone screenshot. no photo nevergiveup.jpg
myphonescreen.png
 
Upvote 0

emexes

Expert
Licensed User
The relevant intent code is highlighted:
B4X:
Sub DownloadAndViewFile(url As String, mime As String)
    Dim sf As String = provider.SharedFolder
    Log(sf)
        
    Dim job As HttpJob
    job.Initialize("DownloadJob", Me)
    job.Username = "Tony" ' Replace with your actual username
    job.Password = "TonyTony3" ' Replace with your actual password
    job.Download(url)
    
    Wait For (job) JobDone(job As HttpJob)
    
    Log(job.Success)
    If job.Success Then
        ' Save the downloaded file

        Dim fname As String = url.SubString(url.LastIndexOf("/")+1)
        Log(fname)

        Dim out As OutputStream = File.OpenOutput(sf, fname, False)
        File.Copy2(job.GetInputStream, out)
        out.Close

        Log(File.Combine(sf, fname))
        Log(File.Size(sf, fname))
    Else
        Log("Error downloading file: " & job.ErrorMessage)
    End If
    job. Release

    Dim uri As Object = provider.GetFileUri(fname)
    Log(uri)
    
    Dim FLAG_GRANT_READ_URI_PERMISSION As Int = 0x00000001
    Dim FLAG_ACTIVITY_NO_HISTORY As Int = 0x40000000
    
    Dim i As Intent
    i.Initialize(i.ACTION_VIEW, uri)
    i.SetType(mime)
    i.Flags = Bit.Or(FLAG_GRANT_READ_URI_PERMISSION, FLAG_ACTIVITY_NO_HISTORY)
    i.WrapAsIntentChooser("Choose Viewer")
    
    StartActivity(i)
End Sub

Sub Button1_Click
    '''DownloadAndViewFile("https://redlightcemetery.com/lara/index.html", "text/html")
    DownloadAndViewFile("https://redlightcemetery.com/lara/nevergiveup.jpg", "image/jpeg")
    '''DownloadAndViewFile("https://redlightcemetery.com/lara/max7219.pdf", "application/pdf")
End Sub

Log output:
Logger connected to:  HMD Global Nokia C01 Plus
--------- beginning of main
--------- beginning of system
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
Using FileProvider? true
** Activity (main) Resume **
/data/user/0/b4a.example/files/shared
*** Receiver (httputils2service) Receive (first time) ***
true
nevergiveup.jpg
/data/user/0/b4a.example/files/shared/nevergiveup.jpg
71244
content://b4a.example.provider/name/nevergiveup.jpg
** Activity (main) Pause, UserClosed = false **
 

Attachments

  • DownloadPasswordedFile3.zip
    10.1 KB · Views: 9
Upvote 0
Top