.setValue和.addValueListener上的错误

时间:2018-08-24 16:38:23

标签: java android

关于如何将图像保存在Firebase存储器中,我有一些编码错误。并出现2个错误...有人可以帮我...我的logcat如下...

1)错误:找不到符号方法setValue(String)

2)错误:找不到符号方法addValueEventListener()

storageImage.child("Tuition Image").setValue(downloadUrl)
                                .addOnCompleteListener(new OnCompleteListener<Void>()
                                {
                                    @Override
                                    public void onComplete(@NonNull Task<Void> task)
                                    {
                                        if (task.isSuccessful())
                                        {
                                            Intent ViewIntent = new Intent(AddAdsActivity.this, AddAdsActivity.class);
                                            ViewIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                            startActivity(ViewIntent);
                                            finish();

                                            Toast.makeText(AddAdsActivity.this, "Tuition image has been successfully stored in Firebase Database", Toast.LENGTH_SHORT).show();
                                            loadingBar.dismiss();
                                        }

                                        else
                                        {
                                            String message = task.getException().getMessage();
                                            Toast.makeText(AddAdsActivity.this, "Error occured: " + message, Toast.LENGTH_SHORT).show();
                                            loadingBar.dismiss();
                                        }
                                    }
                                });

还有第二个错误...

storageImage.child(currentUserID).addValueEventListener(new ValueEventListener()
    {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot)
        {
            if(dataSnapshot.exists())
            {
                image = dataSnapshot.child("Tuition Image").getValue().toString();
                Picasso.get().load(image).placeholder(R.drawable.camera).into(TuitionImage);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError)
        {

        }
    });

2 个答案:

答案 0 :(得分:1)

解决方案:

首次上传图片:

private void uploadImageAndGetURL(String ImageId) {

    final StorageReference filePath = storageImage.child(ImageId + ".jpg");

    UploadTask uploadTask = filePath.putFile(imageUri);

    uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
        @Override
        public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
            if (!task.isSuccessful()) {
                throw task.getException();
            }
            // Continue with the task to get the download URL
            return filePath.getDownloadUrl();
        }
    }).addOnCompleteListener(new OnCompleteListener<Uri>() {
        @Override
        public void onComplete(@NonNull Task<Uri> task) {

            if (task.isSuccessful()) {

                downloadURL = task.getResult().toString();

                storeDataToFirebase();

            } else {

                Toast.makeText(AddAdsActivity.this, "There has bean a problem in the database.", Toast.LENGTH_SHORT).show();
                loadingBar.dismiss();

            }
        }
    });

}

然后上传数据:

private void storeDataToFirebase() {

    String id = databaseAdvertisement.push().getKey();
    Advertisement advertisement = new Advertisement(id, downloadURL, tuitioname, providername, providergender, tuitionaddress, tuitionyear, tuitioncontactnumber, tuitionemail, tuitionsubject, tuitionprice, tuitionclasssize);

    databaseAdvertisement.child(id).setValue(advertisement)
            .addOnSuccessListener(new OnSuccessListener<Void>()
            {
                @Override
                public void onSuccess(Void aVoid)
                {
                    Toast.makeText(AddAdsActivity.this, "Advertisement added successfully!", Toast.LENGTH_SHORT).show();

                    Intent ViewAdsIntent = new Intent(AddAdsActivity.this, ViewAdsActivity.class);
                    ViewAdsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(ViewAdsIntent);
                    finish();

                    loadingBar.dismiss();
                }
            })
            .addOnFailureListener(new OnFailureListener()
            {
                @Override
                public void onFailure(@NonNull Exception e)
                {
                    Toast.makeText(AddAdsActivity.this, "Failed to save the advertisement data. Please try again.", Toast.LENGTH_LONG).show();
                    loadingBar.dismiss();
                }
            });

}

就这样。

答案 1 :(得分:0)

如何解决:确保它们采用OnCreate方法或OnStart方法,并检查您的storageImage类型是否正确

第一

DatabaseReference TuitionReference =storageImage.child("Tuition Image");

TuitionReference.setValue(downloadUrl)
    .addOnCompleteListener(new OnCompleteListener<Void>()
    {
        @Override
        public void onComplete(@NonNull Task<Void> task)
        {
            if (task.isSuccessful())
            {
                Intent ViewIntent = new Intent(AddAdsActivity.this, AddAdsActivity.class);
                ViewIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(ViewIntent);
                finish();
                Toast.makeText(AddAdsActivity.this, "Tuition image has been successfully stored in Firebase Database", Toast.LENGTH_SHORT).show();
                loadingBar.dismiss();
            }

            else
            {
                String message = task.getException().getMessage();
                Toast.makeText(AddAdsActivity.this, "Error occured: " + message, Toast.LENGTH_SHORT).show();
                loadingBar.dismiss();
            }
        }
    });

第二

DatabaseReference reference = database.getReference("Stats").child("Matematik");
reference.addValueEventListener(new ValueEventListener()
    {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot)
        {
            if(dataSnapshot.exists())
            {
                image = dataSnapshot.child("Tuition Image").getValue().toString();
                Picasso.get().load(image).placeholder(R.drawable.camera).into(TuitionImage);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError)
        {

        }
    });