当前位置: 首页 > 知识库问答 >
问题:

Firebase的实时数据库无法从android工作室工作,我无法上传?

易和怡
2023-03-14

我已经修复了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存储规则工作正常,因为图像已上载并调用onSuccessListener。

我用来在数据库上设置值的代码如下所示,我已经将引用定义为:

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

共有2个答案

梁福
2023-03-14

StorageReference filepath=storageReference.child(“Blogimage”).child(System.currentTimeMillis() “.” getFileEXt(imageuri));uploadTask=filepath.putFile(imageuri);

               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);
                                   }
                               });
                           }
                       }
太叔航
2023-03-14

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

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 refrence of current process in this uploadtask varriable 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 runnig 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();
        }
    }

我在上传到数据库时取得了成功。请立即检查您的数据库

 类似资料:
  • 我被鼓励开始android开发,所以我安装了android studio,并尝试启动Hello-world项目。我安装了android studio,它下载了所有必需的文件。我在android studio网站上遵循了guide,但每次尝试创建一个空活动-项目时,我都有很多错误。我试着改变SDK版本,但我对java和android开发完全是新手,不能理解我做错了什么。拜托,我花了一整天的时间来解决

  • 我正试图从MySQL Workbench导出数据库,但在导出过程中我得到了以下信息: 正在运行:mysqldump.exe-默认值-file = " c:\ users \ user \ appdata \ local \ temp \ tmp 2h 91 wa . CNF "-user = root-host = localhost-protocol = TCP-port = 3306-defa

  • 这是我第一次使用Android Studio,我收到错误 “无法解析符号AppCompat活动”和其他符号错误。 我如何解决它?我试过给gradle添加一些东西,但没有效果。还尝试了“使缓存失效/重新启动”,但也没有成功。 这是gradle文件中的依赖项

  • 我正在尝试用Firebase制作一个简单的Android应用程序。 目前,我只是试图从Firebase实时保存用户数据库。但由于某种原因它不起作用。另一方面,身份验证工作完美。 数据库本身没有显示任何内容。它总是显示“null” 我一直在关注这个YouTube教程,并一直在做一些类似的事情。 当我尝试创建一个用户并将其发送到数据库时,运行的程序没有给出任何答案。我有一个进度条,一旦setValue

  • 每次运行代码时都会得到一个NullPointerException。我在代码中启用了Internet和GPS权限(ACCESS_FINE_LOCATION、ACCESS_MOCK_LOCATION、&ACCESS_COARSE_LOCATION)。我试过模拟器和电话,都失败了。这不是全球定位系统或互联网不可用的问题。 我的代码段:- 当我试图获取纬度或经度时,它抛出NullPointer异常。我该

  • 问候flutter开发人员正在开发一个应用程序,iOS和Android使用手机身份验证用户一切正常iOS但Android当这一行被称为我得到一个错误: 错误: E/flutter(14190):[ERROR: flutter/shell/通用/shell.cc(184)]Dart错误:未处理的异常: E/flutter(14190): PlatformExc0019(错误,空引用,空)E/flut