Reputation: 253
My situation: Within an Add a Profile page, I have a spinner, and within that spinner I have a list of images that the user can select from. When a user chose an image and clicks the 'Save' button, I would like that image to go to the database so I could then add it on another activity.
The reason I want to save the image to the database is because I want to add the image that the user selected on my Home Page.
I've tried so many things, including: Using strings inside the spinner instead of images and then when a user selects a certain string it changes an image view on the page to the correct image. But then I still have the problem of getting an image to the database. I've also tried bypassing the database and just adding the image into the other activity with IF statements and Intent etc with no luck.
My custom spinner:
int[] array = getResources().getIntArray(R.array.images_array);
String [] objects = new String[array.length];
for(int i = 0; i != array.length; i++){
objects[i] = "" + array[i];
}
spinner5.setAdapter(new MyAdapter(AddProfileActivity.this, R.id.spinner5, objects));
}
Then under the save button I've tried to use: String valueOfSelectedPos = spinner5.getSelectedItem().toString();
I've tried most variations of this line as I know I'm using a String here to try to add an image!
I then go and add it to my database using:
String id = databaseClients.push().getKey();
Clients clients = new Clients(id, name, career,
valueOfSelectedPos );
databaseClients.child(id).setValue(clients);
As you can probably tell, I'm completely stumped here. If you need any clarification or any more code added, I'll be more than happy to post it. Any help will be appreciated. Thanks in advance!
Upvotes: 0
Views: 3719
Reputation: 785
get image Uri you want to save to firebase, for that you might have to save image to device storage and then call this method
private void uploadImage(Uri uri) {
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReference().child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
storageRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).putFile(uri);
}
Upvotes: 2