How to upload an image to Firebase storage?

I upload images using this code :

private void uploadFile(Bitmap bitmap) {
            FirebaseStorage storage = FirebaseStorage.getInstance();
            StorageReference storageRef = storage.getReferenceFromUrl("Your url for storage");
            StorageReference mountainImagesRef = storageRef.child("images/" + chat_id + Utils.getCurrentTimeStamp() + ".jpg");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 20, baos);
            byte[] data = baos.toByteArray();
            UploadTask uploadTask = mountainImagesRef.putBytes(data);
            uploadTask.addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    // Handle unsuccessful uploads
                }
            }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
                    Uri downloadUrl = taskSnapshot.getDownloadUrl();
                    sendMsg("" + downloadUrl, 2);
                    Log.d("downloadUrl-->", "" + downloadUrl);
                }
            });

        }

Dependency :

Project Level Gradel : classpath 'com.google.gms:google-services:3.0.0'

App Level Gradel : compile 'com.google.firebase:firebase-storage:9.0.2'


You either need to sign-in the user or change the security rules to allow public access. This is explained in the documentation for Firebase Storage Security.

For initial development, you can change the rules at the Firebase Console to allow public access:

service firebase.storage {
  match /b/project-XXXXXXXXXXXXXX.appspot.com/o {
    match /{allPaths=**} {
      // Provide access to all users
      allow read: if true;
      allow write: if true;
    }
  }
}