我正在尝试将此对象上传到Firebase实时数据库:
public class DecorationRequest {
private String mName;
private String mRooms;
private String mBudget;
private String mArea;
private String mDescription;
private List<Uri> mPhotoUrls;
public DecorationRequest() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public DecorationRequest(String name, String rooms, String budget, String area, String description, List<Uri> photoUrls) {
mName = name;
mRooms = rooms;
mBudget = budget;
mArea = area;
mDescription = description;
mPhotoUrls = photoUrls;
}
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
}
public String getRooms() {
return mRooms;
}
public void setRooms(String rooms) {
mRooms = rooms;
}
public String getBudget() {
return mBudget;
}
public void setBudget(String budget) {
mBudget = budget;
}
public String getArea() {
return mArea;
}
public void setArea(String area) {
mArea = area;
}
public String getDescription() {
return mDescription;
}
public void setDescription(String description) {
mDescription = description;
}
public List<Uri> getPhotoUrls() {
return mPhotoUrls;
}
public void setPhotoUrls(List<Uri> photoUrls) {
mPhotoUrls = photoUrls;
}
}
我有一个名为FillInformationActivity的活动,其中包含以下代码:
mSubmitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(checkFilledInformation()){
DecorationRequest decorationRequest = new DecorationRequest(name,
mRoomsPicker.getText().toString(),
mEditBudget.getText().toString(),
mPickArea.getText().toString(),
mEditDescription.getText().toString(),
pictures);
HelperTools.sendHelpRequest(mActivity, decorationRequest);
}
}
});
其在名为“助手工具”的类中调用方法,以下是方法:
public static void sendHelpRequest(final AppCompatActivity activity, DecorationRequest decorationRequest){
mRequestDatabaseReference.push().setValue(decorationRequest);
List<Uri> pictures = decorationRequest.getPhotoUrls();
for(int i = 0; i < pictures.size(); i++){
final StorageReference photoRef = mChatPhotosStorageReference.child(pictures.get(i).getLastPathSegment());
Task<Uri> urlTask = photoRef.putFile(pictures.get(i)).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();
} else {
return photoRef.getDownloadUrl();
}
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if(task.isSuccessful()){
Uri downloadUri = task.getResult();
ChatMessage chatMessage =
new ChatMessage(mUserName, getTime(), null, downloadUri.toString());
mMessagesDatabaseReference.push().setValue(chatMessage);
} else {
Toast.makeText(activity.getApplicationContext(), "Failure", Toast.LENGTH_LONG).show();
}
}
});
}
}
最后是错误日志。从这个循环的大约5000行开始:
at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzkt.zzl(Unknown Source)
at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source)
然后以这个结尾:
at com.google.firebase.database.DatabaseReference.zza(Unknown Source)
at com.google.firebase.database.DatabaseReference.setValue(Unknown Source)
at com.example.tino.interiordecoration.HelperTools$override.sendHelpRequest(HelperTools.java:75)
at com.example.tino.interiordecoration.HelperTools$override.access$dispatch(HelperTools.java)
at com.example.tino.interiordecoration.HelperTools.sendHelpRequest(HelperTools.java)
at com.example.tino.interiordecoration.FillInformationActivity$2.onClick(FillInformationActivity.java:82)
at android.view.View.performClick(View.java:5706)
at android.view.View$PerformClick.run(View.java:22822)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6301)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1084)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:945)
HelperTools的第75行是此行:
mRequestDatabaseReference.push().setValue(decorationRequest);
如果我将对象DecorationRequest更改为一些随机字符串,则代码会运行,并且文件确实会上传到Firebase实时数据库,但这显然不是我想要的。
答案 0 :(得分:1)
Uri类不是Firebase支持的数据类型。列表应包含String对象,而不应包含Uri对象。
我的代码只要更改就行了
public List<Uri> getPhotoUrls() {
return mPhotoUrls;
}
对此
public List<String> getPhotoUrls() {
List<String> photoUrlStrings = new ArrayList<String>();
for(int i=0; i<mPhotoUrls.size(); i++){
photoUrlStrings.add(mPhotoUrls.get(i).toString());
}
return photoUrlStrings;
}