Android Question Download and save the file

medsport2012

Member
Licensed User
Longtime User
Hi
I'm trying to save the downloaded file with this code:
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 = True Then
        Dim out As OutputStream = File.OpenOutput(File.DirInternal, "Stop.mp3", False)
        File.Copy2Async(j.GetInputStream, out)
        out.Close
        Log("Success")
    Else
        Log("Fail")
    End If
    j.Release
End Sub
but nothing happens, If i write the path to the folder:
B4X:
Dim out As OutputStream = File.OpenOutput("/storage/emulated/0/LS2018", "Stop.mp3", False)
I'am getting this error:
2021-06-20_11-38-43.png
 

medsport2012

Member
Licensed User
Longtime User
Yes, i got "Success", but the file wasn't saved, so problem is in saving.
Error:
Error occurred on line: 51 (Main)
java.io.FileNotFoundException: /storage/emulated/0/LS2018/Stop.mp3: open failed: EPERM (Operation not permitted)
at libcore.io.IoBridge.open(IoBridge.java:492)
at java.io.FileOutputStream.<init>(FileOutputStream.java:236)
at anywheresoftware.b4a.objects.streams.File.OpenOutput(File.java:449)
at b4a.example.main$ResumableSub_DownloadAndSaveFile.resume(main.java:499)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
at anywheresoftware.b4a.BA$2.run(BA.java:387)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8506)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: android.system.ErrnoException: open failed: EPERM (Operation not permitted)
at libcore.io.Linux.open(Native Method)
at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:254)
at libcore.io.ForwardingOs.open(ForwardingOs.java:166)
at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:8367)
at libcore.io.IoBridge.open(IoBridge.java:478)
... 22 more
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Yes, i got "Success", but the file wasn't saved, so problem is in saving.
It IS saved! In DirInternal.

But you are trying to save it to another place. This is the issues you have to start with,
As the error point out you do not have the permission to write to the path you want.

 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Dim out As OutputStream = File.OpenOutput("/storage/emulated/0/LS2018", "Stop.mp3", False)
From the looks of the path, it appears that it refers to File.DirExternal which requires runtime permission or in newer Android OS version you may not even be able to access the secondary storage at all. but the proper place to save it is File.DirInternal ( equivalent to XUI.DefaultFolder) which requires no permission.
Take a look here where you might be able to save it:
 
Upvote 0

medsport2012

Member
Licensed User
Longtime User
It IS saved! In DirInternal.

But you are trying to save it to another place. This is the issues you have to start with,
As the error point out you do not have the permission to write to the path you want.

I tried to find the file in explorer, but it isn't there, how can i find it?
 
Upvote 0

medsport2012

Member
Licensed User
Longtime User
It IS saved! In DirInternal.

But you are trying to save it to another place. This is the issues you have to start with,
As the error point out you do not have the permission to write to the path you want.

Thaks a lot, i managed to save the file but it's empty.
 
Upvote 0

eric19740521

Member
Licensed User
Longtime User

Attachments

  • t01.zip
    9.8 KB · Views: 167
Last edited:
Upvote 0
Top