What would be a good way to retrieve and display user-specific images that are stored in firebase storage?

Viewed 631

I'm making an Android app that displays a user's profile info. I am able to store the images on Cloud Storage, but I am not sure of how to retrieve and display the user-specific images that are stored in Cloud Storage.

One of the main problems I am facing is that user.getPhotoUrl() is not giving me the right uri. It gives me something of the form com.google.android.gms. and so on.

I have tried using Glide to display the images. But, it did not work as I was getting a FileNotFoundException. So, I tried using Glide with StorageReference.

But, the problem with using StorageReference is that I have to explicitly provide the uri of the image, while I want to retrieve the image specific to a user.

I expected the profile picture selected by the user to show up on the profile page, but when I run the app on Android Studio, nothing shows up.

1 Answers

I wrote a method which upload bitmap to firebase storage in android and retrieve it url, please use this method to upload image and retrieve url, After that let me know if you face any issue.

public static void UploadBitmap(Activity activity, Bitmap bitmap, String serverFileName) {

        if (bitmap == null) {
            return;
        }

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {

                FirebaseStorage storage = FirebaseStorage.getInstance();
                StorageReference storageRef = storage.getReference();
                // Create a reference to the file you want to upload
                String directory = "images/";

                StorageReference fileRef = storageRef.child(directory + serverFileName);

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] data = stream.toByteArray();

                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {


                        UploadTask uploadTask = fileRef.putBytes(data);
                        uploadTask.addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception exception) {
                                // Handle unsuccessful uploads
                                Log.e("oops","error in bitmap uploading");

                            }
                        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                // taskSnapshot.getMetadata() contains file metadata such as size, content-type, etc.
                                // ...
                                // now download url first
                            }
                        });

                        Task<Uri> urlTask = uploadTask.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();
                                }

                                // Continue with the task to get the download URL
                                return fileRef.getDownloadUrl();
                            }
                        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                            @Override
                            public void onComplete(@NonNull Task<Uri> task) {
                                if (task.isSuccessful()) {
                                    Uri downloadUri = task.getResult();
                                    String stringUrl = downloadUri.toString();

                                    // Great image is uploaded and url is here, you can load image anywhere via this stringUrl with Glide or Picasso.

                                } else {
                                    // Handle unsuccessful uploads
                                    Log.e("oops","error in url retrieval");
                                }
                            }
                        });


                    }
                });


            }
        });
        thread.start();




    }
Related