Firebase的真实的时间数据库不工作从android工作室我不能上传吗?

jgwigjjp  于 12个月前  发布在  Android
关注(0)|答案(2)|浏览(94)

我已经修复了firebase实时数据库的读取和正确规则,但我仍然无法读取和写入它。

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

字符串
我也试过这个:

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


Firebase存储规则:

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


Firebase存储规则工作正常,因为图像已上载,并已调用。
下面是我用来在数据库上设置值的代码,我已经定义了引用:

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();
        }
    }


事件创建成功的吐司每次都显示,图像也上传到存储,但对象不保存在实时数据库中。而事件创建用户事件只是一个类,我想保存到实时数据库的对象沿着我将上传到存储的图像。图像总是在Firebase存储上传成功,并在它之后onSuccess函数被调用,并在onSuccess函数中我已经写了代码为对象保存实时数据库,但这不工作。请帮助我解决它,我已经浪费了2天我的,但不能解决这个问题。

yacmzcpb

yacmzcpb1#

好的......我已经试过你的代码了。下面是修改后的函数:
第一个月

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();
        }
    }

字符串
我正在成功上传到数据库。请检查您的数据库现在。

4zcjmb1e

4zcjmb1e2#

Copyright © 2018 - 2019 www.hjk.com All Rights Reserved版权所有

filepath.putFile(imageuri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                   @Override
                   public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                     // String downlod = taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();
                       if (taskSnapshot.getMetadata() != null) {
                           if (taskSnapshot.getMetadata().getReference() != null) {
                               Task<Uri> result = taskSnapshot.getStorage().getDownloadUrl();
                               result.addOnSuccessListener(new OnSuccessListener<Uri>() {
                                   @Override
                                   public void onSuccess(Uri uri) {
                                       String imageUrl = uri.toString();
                                       DatabaseReference newpost=databaseReference.push();
                                       Map<String , String > profile =new HashMap<>();
                                       profile.put("name",name);
                                       profile.put("prof",prof);
                                       profile.put("location",location);
                                       profile.put("email",mail);
                                       profile.put("web",web);
                                       profile.put("uid",user.getUid());
                                       profile.put("url",imageUrl);

                                       allUserMember.setName(name);
                                       allUserMember.setUid(auth.getUid());

                                       allUserMember.setProf(prof);
                                       allUserMember.setLocation(location);

                                       allUserMember.setUrl(imageUrl);
                                       newpost.setValue(profile);
                                       newpost.child(user.getUid()).setValue(allUserMember);
                                       documentReference.set(profile).addOnSuccessListener(new OnSuccessListener<Void>() {
                                           @Override
                                           public void onSuccess(Void unused) {

                                               Snackbar.make(findViewById(android.R.id.content),"Image uplode",Snackbar.LENGTH_LONG).show();
                                               pd.dismiss();
                                               Toast.makeText(CreateProfile1.this, "it work", Toast.LENGTH_SHORT).show();
                                           }
                                       });
                                       //createNewPost(imageUrl);
                                   }
                               });
                           }
                       }

字符串

相关问题