0

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!

1 Answer 1

2

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); } 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer! So I would have to first have the images saved to the device? Is there a way around this do you know? Thanks again.
what is the type of image file you have? is it a drawable or bitmap?
I can do either, currently it's a drawable. Do you know for either? Thanks
if you want the image file to be used then only upload the file or else you can just upload the string of resource id ,store it and retrieve it to show it in imageview

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.