我已经解决了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();
}
}
事件创建的Toast每次都会成功显示,并且图像也会上载到存储中,但是该对象不会保存在实时数据库中。而且CreateUserEvent只是一个类,我想将其对象与要上传到存储的图像一起保存到实时数据库中。映像始终可以成功上传到Firebase存储上,并且在调用onSuccess函数并在onSuccess函数内部后,我已经为对象编写了代码以保存在实时数据库中,但这不起作用。请帮助我解决它,我已经浪费了2天,但无法解决此问题。
答案 0 :(得分:0)
好吧...我已经尝试过您的代码了。下面是修改后的函数:
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();
}
}
上传到数据库时,我获得了成功。请立即检查您的数据库。
答案 1 :(得分:0)
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);
}
});
}
}