1

I'm trying to upload this object to Firebase realtime database:

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

I have an activity called FillInformationActivity which has this code in it:

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

And its calling the method in a class called Helper tools, here is the method:

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

And finally here is the error log. It starts with literally about 5000 lines of this looping:

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)

And then ends with this:

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)

The line 75 at HelperTools is this line:

mRequestDatabaseReference.push().setValue(decorationRequest);

If I change the object DecorationRequest to some random string, the code runs, and the file does upload to the Firebase Realtime Database, but obviously that is not what I want.

André Kool
  • 4,880
  • 12
  • 34
  • 44
Tino Terävä
  • 75
  • 1
  • 2
  • 11

1 Answers1

1

The Uri class is not a data type that is supported in Firebase. The List should contain String objects and not Uri objects.

My code works when I simply change this

public List<Uri> getPhotoUrls() {
    return mPhotoUrls;
}

to this

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;
}
André Kool
  • 4,880
  • 12
  • 34
  • 44
Tino Terävä
  • 75
  • 1
  • 2
  • 11