Hussain
Hussain

Reputation: 79

How to Link the Image URL in fire base storage to database under specific user in Android

I am new on fire base. I am working on an Application. I have uploaded the profile image and user data to fire base.But now i a confused how to related the profile pictures to specific user. For example Name= Hussain , Contact=########### and the profile image in storage is 112313.jpg. Another user is name=Ali, contact=######## and profile image=1323.jpg. So, the question is how i can get the image of hussain under hussain detail, and liaqat's image under liaqat detail. Thanks in advance

Upvotes: 0

Views: 1951

Answers (1)

Nagaraj N
Nagaraj N

Reputation: 467

Once You have uploaded your profile image to firebase cloud storage, you can get the URL of that. Then, You can update your profile image URL to that particular users details in Real time database. Please look at the code below,

    // File or Blob
    file = Uri.fromFile(new File("path/to/mountains.jpg"));

    // Create the file metadata
    metadata = new StorageMetadata.Builder()
            .setContentType("image/jpeg")
            .build();

    // Upload file and metadata to the path 'images/mountains.jpg'
    uploadTask = storageRef.child("images/"+file.getLastPathSegment()).putFile(file, metadata);

    // Listen for state changes, errors, and completion of the upload.
    uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            // Handle successful uploads on complete
            Uri downloadUrl=taskSnapshot.getMetadata().getDownloadUrl();

FirebaseDatabase.getInstance().getReference('users').child('hussain_UID')
    .child('photoURL')
    .set(downloadUrl.toString());

        }
    });

Hope this helps your query.

Upvotes: 1

Related Questions