Android Question Unable to Download a File using Firebase Storage

beelze69

Active Member
Licensed User
Longtime User
Hi,

I am using Firebase Storage to download a file from Firebase Console.

I modified the example FirebaseStorageExample provided by Erel on link

https://www.b4x.com/android/forum/threads/firebasestorage-simple-file-storage-backend.68350/

Suitably.

My aim: To download a file sampleFBFile.jpg in the public folder under Firebase Storage to my
Dir.InternalCache.


The Rules in the Firebase Storage console is

service firebase.storage {
match /b/beelzetestproject.appspot.com/o {
match /{allPaths=**} {
allow read;
allow write: if request.auth != null;}


I have downloaded the constructed google-services.json file in mobile application folder.

The application compiles properly.

But when I run the example I get the message 'Unfortunately B4A.example has stopped'.

The log dump is :

<LOG DUMP>
connection failed.
*** Service (starter) Create ***
FirebaseAuth initialized
starter_service_create (java line: 159)
java.lang.IllegalArgumentException: The storage Uri cannot contain a path element.
at com.google.firebase.storage.FirebaseStorage.zza(Unknown Source)
at com.google.firebase.storage.FirebaseStorage.getInstance(Unknown Source)
at com.google.firebase.storage.FirebaseStorage.getInstance(Unknown Source)
at anywheresoftware.b4x.objects.FirebaseStorageWrapper.Initialize(FirebaseStorageWrapper.java:47)
at b4a.myTestProject.starter._service_create(starter.java:159)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at b4a.myTestProject.starter.onCreate(starter.java:56)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2571)
at android.app.ActivityThread.access$1600(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:4987)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Unable to create service b4a.myTestProject.starter: java.lang.RuntimeException: java.lang.IllegalArgumentException: The storage Uri cannot contain a path element.
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2593)
at android.app.ActivityThread.access$1600(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:4987)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.lang.IllegalArgumentException: The storage Uri cannot contain a path element.
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:223)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at b4a.myTestProject.starter.onCreate(starter.java:56)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2571)
... 10 more
Caused by: java.lang.IllegalArgumentException: The storage Uri cannot contain a path element.
at com.google.firebase.storage.FirebaseStorage.zza(Unknown Source)
at com.google.firebase.storage.FirebaseStorage.getInstance(Unknown Source)
at com.google.firebase.storage.FirebaseStorage.getInstance(Unknown Source)
at anywheresoftware.b4x.objects.FirebaseStorageWrapper.Initialize(FirebaseStorageWrapper.java:47)
at b4a.myTestProject.starter._service_create(starter.java:159)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
... 13 more
main_btndownloadpublic_click (java line: 363)
java.lang.IllegalArgumentException: The supplied bucketname does not match the storage bucket of the current instance.
at com.google.android.gms.common.internal.zzbq.checkArgument(Unknown Source)
at com.google.firebase.storage.FirebaseStorage.zzt(Unknown Source)
at com.google.firebase.storage.FirebaseStorage.getReferenceFromUrl(Unknown Source)
at anywheresoftware.b4x.objects.FirebaseStorageWrapper.DownloadStream(FirebaseStorageWrapper.java:107)
at anywheresoftware.b4x.objects.FirebaseStorageWrapper.DownloadFile(FirebaseStorageWrapper.java:100)
at b4A.RamanFirebaseStorage.main._btndownloadpublic_click(main.java:363)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
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:80)
at android.view.View.performClick(View.java:3538)
at android.view.View$PerformClick.run(View.java:14330)
at android.os.Handler.handleCallback(Handler.java:608)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:4987)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)


<END LOG DUMP>

My code:

B4X:
..
..

Sub btnDownloadPublic_Click
    'Msgbox("My internalcache is :" & File.DirInternalCache,"TEST") 
    'You need to first upload a file from Firebase console.
    Starter.storage.DownloadFile("/public/sampleFBFile.jpg", File.DirInternalCache, "sampleFBFile.jpg")
End Sub

Sub btnUploadAuth_Click
   
End Sub

Sub Storage_DownloadCompleted (ServerPath As String, Success As Boolean)
    ToastMessageShow($"DownloadCompleted. Success = ${Success}"$, True)
    If Not(Success) Then Log(LastException)
End Sub

Sub Storage_UploadCompleted (ServerPath As String, Success As Boolean)
    ToastMessageShow($"UploadCompleted. Success = ${Success}"$, True)
    If Not(Success) Then Log(LastException)
End Sub

..
..

Ps. help.. Thanks
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

beelze69

Active Member
Licensed User
Longtime User
So you are using gs://beelzetestproject.appspot.com while initializing the storage?


Where is the starter code for the Storage?
Hi !

My starter page is as below:
B4X:
Sub Process_Globals
    Public auth As FirebaseAuth
    Public storage As FirebaseStorage
    Private bucket As String = "gs://beelzetestproject.appspot.com"
End Sub

Sub Service_Create
    auth.Initialize("auth")
    storage.Initialize("storage", bucket)
End Sub

Private Sub Auth_SignedIn (User As FirebaseUser)
    Log($"Signed in: ${User}"$)
    CallSub(Main, "SignedIn")
End Sub


Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub
 
Upvote 0
Top