androidfirebasefirebase-realtime-databasefirebase-storagerules

Real time Database of Firebase not working from android studio i can't able to upload on it?


I have already fix the read and right rules on realtime database of firebase still i am unable to read and write on it.

{
  "rules": {
    ".read": "true",
    ".write": "true"
  }
}

And i have also tried this :

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

Firebase Storage Rule:

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

Firebase Storage Rule is Working Correctly because the image gets uploaded and onSuccessListener get called.

The Code Which i am using to setValue on database is shown below and I have already defined references as :

    storageReference = FirebaseStorage.getInstance().getReference("Events_Details");
    databaseReference = FirebaseDatabase.getInstance().getReference("Events_Details");

    private void uploadUserInformationToDatabase() {
        progressDialog.show();
        if (image_uri != null) {

            //this will create a big_number.jpg and when we call .child this means we are
            //going to add something inside Events_Images Directory
            StorageReference fileReference = storageReference.child(System.currentTimeMillis() + "." + getFileExtension(image_uri));

            uploadTask = fileReference.putFile(image_uri)
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                            //Now we need to get the url of the image that you have uploaded.
                            Task<Uri> uri = taskSnapshot.getStorage().getDownloadUrl();
                            while (!uri.isComplete());
                            String url = uri.getResult().toString();
                            createUserEvent.setImageUrl(url);

                            //now we will save this object in our database
                            String uploadId = databaseReference.push().getKey();
                            databaseReference.child(uploadId)
                                             .setValue(createUserEvent);
                            progressDialog.dismiss();

                            Toast.makeText(context, "Event Created Successfully", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
                            progressDialog.dismiss();
                        }
                    });

        } else {
            progressDialog.dismiss();
            Toast.makeText(context, "No File Selected", Toast.LENGTH_SHORT).show();
        }
    }

Toast for Event Created Successfully displays every time and image also gets uploaded to storage but the object doesn't save on realtime database. And CreateUserEvent is just a class, the object of which i want to save to realtime database along with the image which i will upload to storage. The image always get success in uploading on firebase storage and after it onSuccess function gets called and inside onSuccess function i have written Code for object to save on realtime database but that doesn't work.Please help me to solve it i have already wasted 2 days of mine but can't able to fix this problem. enter image description here


Solution

  • Ok......I have tried your code. Below is the modified function:

    databaseReference = FirebaseDatabase.getInstance().getReference();

    private void uploadUserInformationToDatabase() {
            progressDialog.show();
    
            if (image_uri != null) {
    
                //this will create a big_number.jpg and when we call .child this means we are
                //going to add something inside Events_Images Directory
                StorageReference fileReference = storageReference.child(System.currentTimeMillis() + "." + getFileExtension(image_uri));
    
                //now we will store our image file in firebase and check for success and failure events
                //And we store the reference of current process in this uploadtask variable which helps us
                //when user clicks on upload button multiple time, so when he clicks one time uploadTask will
                //take  the reference and when the upload running and the user clicks the upload button another
                //time then we put a check if uploadTask is null or not. it is null then this means no task is
                //running else we don't upload. This check you put above in upload onlicklisterner.
    
    
                uploadTask = fileReference.putFile(image_uri)
                        .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
    
    //                            lets create the object with name and url of the image and save this object into our database.
    //                            String name_of_event = file_name.getText().toString().trim();
    //                            String url_of_image = taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();
    //                            Upload upload = new Upload(name_of_event, url_of_image);
    
    
    
                                //Now you just need to get the url of the image that you have uploaded.
    
                                Task<Uri> uri = taskSnapshot.getStorage().getDownloadUrl();
                                while (!uri.isComplete()) ;
                                String url = uri.getResult().toString();
                                createUserEvent.setImageUrl(url);
    
                                //now we will save this object in our database
                                String uploadId = databaseReference.push().getKey();
                                Log.d(TAG, "onSuccess: Going To Save Object To Firebase");
                                Log.d(TAG, "onSuccess: UPLOAD ID : "+uploadId);
    
                                databaseReference.child("Event Details").child(uploadId).setValue(createUserEvent).addOnCompleteListener(new OnCompleteListener<Void>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Void> task) {
                                        Toast.makeText(context, "Data uploaded successfully", Toast.LENGTH_SHORT).show();
                                        progressDialog.dismiss();
                                    }
                                }).addOnFailureListener(new OnFailureListener() {
                                    @Override
                                    public void onFailure(@NonNull Exception e) {
                                        Toast.makeText(context, "Failed to upload data", Toast.LENGTH_SHORT).show();
                                        progressDialog.dismiss();
                                    }
                                });
                            }
                        });
            } else {
                progressDialog.dismiss();
                Toast.makeText(context, "No File Selected", Toast.LENGTH_SHORT).show();
            }
        }
    

    I am getting success while uploading to database. Please check your database now.