无法将Map <>存储到Firebase存储中

时间:2019-08-30 12:15:02

标签: android firebase google-cloud-firestore firebase-storage

我有newEventActivity,用户可以在其中创建新事件,然后用户选择图像,然后输入事件的描述。我确信我的代码会被执行,因为对task.isSuccessful的Intent将我转发到Menu.class。但是,eventsMap不会存储在Firebase存储上。

我没有收到任何错误,所以我不确定如何解决此问题。

我也无法将图像存储在存储中,也无法将Map <>存储在Firestore中,因此基本上我的整个方法都行不通。

newEventButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                     final String desc = newEventDesc.getText().toString();

                    if(!TextUtils.isEmpty(desc) && eventImageUri != null){

                        newEventProgress.setVisibility(View.VISIBLE);

                        String imageUUID = UUID.randomUUID().toString();

                        final StorageReference file = storageReference.child("event_images").child(imageUUID + ".jpg");
                        file.putFile(eventImageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                                if(task.isSuccessful()){


                                    String downloadUri = file.getDownloadUrl().toString();

                                    Map<String, Object> eventsMap = new HashMap<>();
                                    eventsMap.put("image_url", downloadUri);
                                    eventsMap.put("desc", desc);

                                    firebaseFirestore.collection("Events").add(eventsMap).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
                                        @Override
                                        public void onComplete(@NonNull Task<DocumentReference> task) {

                                            if(task.isSuccessful()){

                                                Toast.makeText(newEventActivity.this, "Event Was Created", Toast.LENGTH_LONG).show();
                                                Intent intent = new Intent(newEventActivity.this, Menu.class);
                                                startActivity(intent);
                                                finish();

                                            } else {

                                                String error = task.getException().getMessage();
                                                Toast.makeText(newEventActivity.this, error , Toast.LENGTH_LONG).show();


                                            }
                                            newEventProgress.setVisibility(View.INVISIBLE);
                                        }
                                    });

                                } else{

                                    newEventProgress.setVisibility(View.INVISIBLE);





                                }
                            }
                        });


                    }

                }
            });

            }
            @Override
            protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {

                    CropImage.ActivityResult result = CropImage.getActivityResult(data);

                    if (resultCode == RESULT_OK) {

                        eventImageUri = result.getUri();

                                newEventImage.setImageURI(eventImageUri);



                    } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {

                        Exception error = result.getError();
                        Toast.makeText(newEventActivity.this, "Error:" + error, Toast.LENGTH_LONG).show();

                    }
                }
            }
          }

`

private Toolbar newEventToolbar;
    private Button newEventButton;
    private ImageView newEventImage;
    private TextView newEventDesc;
    private ProgressBar newEventProgress;
    private StorageReference storageReference;
    private FirebaseFirestore firebaseFirestore;


    private Uri eventImageUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_event);

        storageReference = FirebaseStorage.getInstance().getReference();
        firebaseFirestore = FirebaseFirestore.getInstance();

        newEventProgress = (ProgressBar) findViewById(R.id.new_event_progress);
        newEventImage = (ImageView) findViewById(R.id.post_image);
        newEventButton = (Button) findViewById(R.id.post_btn);
        newEventToolbar = (Toolbar) findViewById(R.id.new_event_toolbar);
        newEventDesc = (TextView) findViewById(R.id.post_desc); `

0 个答案:

没有答案