13

I use Firebase Storage to upfile. But it does not work THIS IS MY CODE.

FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("gs://the-food-house.appspot.com/");
// Create a reference to "file"
    StorageReference mStorage = storageRef.child("Album Avatar")
            .child(UserUID)
            .child(AvatarUser.getLastPathSegment());
    mStorage.putFile(AvatarUser).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            Toast.makeText(SignUpWithEmail.this, "UPLOAD FILE OK", Toast.LENGTH_SHORT).show();
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.d("ERROR", e.toString());
            Toast.makeText(SignUpWithEmail.this, "Failed", Toast.LENGTH_SHORT).show();
        }
    };

Here is the error I am having:

com.google.firebase.storage.StorageException: An unknown error occurred, please check the HTTP result code and inner exception for server response.

And this is details of error:

Unrecognized GLES max version string in extensions: ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 
E/UploadTask: could not locate file for uploading:https://firebasestorage.googleapis.com/v0/b/the-food-house.appspot.com/o/Avatar%20Default%2Fmale.png?alt=media&token=3f285cab-c32b-4f33-a909-5a85ef62d74d
E/StorageException: StorageException has occurred.
An unknown error occurred, please check the HTTP result code and inner exception for server response.
    Code: -13000 HttpResult: 0 
E/StorageException: No content provider: https://firebasestorage.googleapis.com/v0/b/the-food-house.appspot.com/o/Avatar%20Default%2Fmale.png?alt=media&token=3f285cab-c32b-4f33-a909-5a85ef62d74d
java.io.FileNotFoundException: No content provider: https://firebasestorage.googleapis.com/v0/b/the-food-house.appspot.com/o/Avatar%20Default%2Fmale.png?alt=media&token=3f285cab-c32b-4f33-a909-5a85ef62d74d
    at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1131)
    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:982)
    at android.content.ContentResolver.openInputStream(ContentResolver.java:702)
    at com.google.firebase.storage.UploadTask.<init>(Unknown Source)
    at com.google.firebase.storage.StorageReference.putFile(Unknown Source)
    at thedark.example.com.thefoodhouse.Activity.Authencation.SignUpWithEmail.submitAvatarStorage(SignUpWithEmail.java:111)
    at thedark.example.com.thefoodhouse.Activity.Authencation.SignUpWithEmail.access$1200(SignUpWithEmail.java:38)
    at thedark.example.com.thefoodhouse.Activity.Authencation.SignUpWithEmail$5.onComplete(SignUpWithEmail.java:170)
    at com.google.android.gms.tasks.zzf.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

This is rule firebase:

allow read, write: if request.auth != null;

It has given me a headache these past few days. Hope that someone finds the problem. Help me please. Thank you.

Martin Brisiak
  • 3,872
  • 12
  • 37
  • 51

19 Answers19

11

This Sorted me out, All I needed to do was to update the firebase-storage lib. In my case it was 'com.google.firebase:firebase-storage:16.4.0' and after updating it to 'com.google.firebase:firebase-storage:17.0.0' everything start working fine again.

bastami82
  • 5,955
  • 7
  • 33
  • 44
6

update your firebase-storage dependency

implementation 'com.google.firebase:firebase-storage:17.0.0'
Softgenies
  • 101
  • 1
  • 3
4

This error is generated because of rules defined under rules tab in storage, by deafult the rules will be - rules_version = '2'; service firebase.storage { match /b/{bucket}/o { match /{allPaths=**} { allow read, write if request.auth != null; } } }

if u have not implmented authentication and ttying to store files in storage then implment authentication or just remove it if you are implementing for learning purpose, dont use this in production application.

rules_version = '2'; service firebase.storage { match /b/{bucket}/o { match /{allPaths=**} { allow read, write if true; } } }

  • Thanks, mine default rule for write was "write if false" on a new project, it should've been "write if request.auth != null" so only authenticated users can write. I spent hours trying to debug as previously on new projects, the default was always "write if request.auth != null; ", now it seems default is false – ahmed Feb 27 '22 at 00:22
3

You can't use putFile() with an HTTP type Uri. According to the documentation, you're supposed to use it to upload a local file.

If you want to upload a file to Storage that exists somewhere else referenced by an HTTP URL, you'll have to download that file first, store it locally, then upload it.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
3

So the I had the same problem and I easily fixed it!, so this is not a problem with firebase, but the problem is that your file can not be accessed from your local disk!

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

for api 21+ you will need to request permission, for security reasons, check this article for more information: How to request storage permission

Dumitru
  • 384
  • 4
  • 8
2

Using putStream() is the the recommended way for most files instead of putFile() (for local files on the device) like so:

InputStream stream = new FileInputStream(new File(pathToYourFile)));

UploadTask uploadTask = imageFileStorageReference.putStream(stream);
Oush
  • 3,090
  • 23
  • 22
1

In my case I had to paste the rule below and publish on my storage.

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if true;
    }
  }
}
Steve Moretz
  • 2,758
  • 1
  • 17
  • 31
1

The key to solving this problem is to enable Storage in the app's permissions. You should follow these steps:

Settings -> Apps -> AppName -> Permissions -> Enable Storage

Remember that the file should exist on the device.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
1

Make sure you've connected to firebase. Open the firebase assistant, go to storage, click on upload and download a file from storage and check. Connecting is easy. Just click on the connect button.

AurumTechie
  • 166
  • 3
  • 14
1

Add correct dependencies:

implementation 'com.google.firebase:firebase-auth:17.0.0'
implementation 'com.google.firebase:firebase-storage:17.0.0'
implementation 'com.google.firebase:firebase-database:17.0.0'
1

In my case, I have exceeded the downloading Quota which is of 1GB/day. When I tried next day I could able to download without any issue.

Rohit S
  • 714
  • 5
  • 7
0

May be this is the wrong path bug but in my case resolve this here is the example

i use this code then i received error

public static StorageReference getChildProfileStorage(String vid){
    StorageReference storageReference= FirebaseStorage.getInstance().getReference();
    storageReference.child("ParentDataStore").child(CurrentUser.getInstance().getEmail())
            .child("ChildDataStore").child(vid);

    return storageReference;
}

here is the line that solve my problem

public static StorageReference getStudentProfileStorage(String vid){
    StorageReference storageReference= FirebaseStorage.getInstance().getReference("ParentDataStore")
            .child("StudentDataStore").child(vid).child("profile");
    return storageReference;
}
Ali Raza
  • 21
  • 2
  • 6
0

For me this error was triggered when uploading a 0.0 KB image file; that is because I was testing image compression algorithm that has a bug resulting in 0.0 KB image in some cases.

So, make sure that the image/file you upload is greater than 0 KB.

Zain
  • 37,492
  • 7
  • 60
  • 84
0

For me adding android:requestLegacyExternalStorage="true" in the application tag in the menifest file solved the error. But there can be various reasons for this error, since it is not pointing out the reason. But things can be done. First update the storage library of firebase to latest version in gradle file. Second check permissions in android settings

Sahil Rajpal
  • 510
  • 4
  • 8
0

For me it working properly if the upload file is no symbols such +, @, #, $ make sure the file name is only number, letters, period, and underscore.

Martin Brisiak
  • 3,872
  • 12
  • 37
  • 51
0

An unknown error occurred, please check the HTTP result code and inner exception for server response

I got this again when I tried to download a file from Firebase Storage using .getFile() method, and the reason is that I didn't grant the app WRITE_EXTERNAL_STORAGE permission before doing that.

File dir = new File(Environment.getExternalStorageDirectory(), "subDir");
File file = new File(dir, "fileName");

firebaseStorage.getReference().child("someDirectory").child("fileName").getFile(file);
Zain
  • 37,492
  • 7
  • 60
  • 84
0

I had something similar (not same ) and eventually enabling the write external permission fixed my problem. pay attention - it needs special permission ( to ask from the user , and not only installation permission -began in android 6) .

Gilad Levinson
  • 234
  • 2
  • 12
0

Update BOM; fixed mine problem with this. learn latest version of bom : https://firebase.google.com/docs/android/setup

dependencies {

    .....
    implementation platform('com.google.firebase:firebase-bom:30.1.0')
    implementation 'com.google.firebase:firebase-analytics'
    implementation 'com.google.firebase:firebase-auth'
    implementation 'com.google.firebase:firebase-storage'
    implementation 'com.google.firebase:firebase-firestore'
} 
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
0

In my case it was that i have to change rules version to version 2, i put this on top of storage rules:

rules_version = "2";