Skip to main content

Overview

The ImageSetter class provides a static method to asynchronously load user profile pictures from Firebase Storage into ImageView components. It uses the Glide library for efficient image loading with circular crop transformation.
This utility implements image compression optimization mentioned in the app’s advanced features, reducing bandwidth usage when displaying profile pictures.

Class Definition

ImageSetter.java
package project.avishkar.salesmanagement;

import android.content.Context;
import android.widget.ImageView;
import android.widget.ProgressBar;
import com.bumptech.glide.Glide;
import com.google.firebase.database.FirebaseDatabase;

public class ImageSetter {
    private static DatabaseReference mDatabase = 
        FirebaseDatabase.getInstance().getReference(Constants.DATABASE_UPLOADS);
}

Methods

setImage

public static void setImage(
    final Context context, 
    final ImageView view, 
    final String currEmail, 
    final ProgressBar mSpinner
)
Asynchronously loads a user’s profile image from Firebase Storage based on their email address.
context
Context
required
Application or activity context for Glide image loading
view
ImageView
required
The ImageView where the profile picture will be displayed
currEmail
String
required
The user’s email address used to look up their profile image in Firebase
mSpinner
ProgressBar
required
ProgressBar widget to show loading state while fetching the image

How It Works

  1. Shows Loading Indicator - Sets the ProgressBar to visible
  2. Queries Firebase Database - Searches the uploads node for matching email
  3. Loads Image with Glide - Applies circular crop transformation and placeholder
  4. Hides Loading Indicator - Removes ProgressBar when complete
mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for(DataSnapshot snapshot : dataSnapshot.getChildren()){
            Upload upload = snapshot.getValue(Upload.class);
            if(upload.getEmail().equals(currEmail)){
                Glide.with(context)
                    .load(upload.getUrl())
                    .apply(RequestOptions.circleCropTransform()
                        .placeholder(R.mipmap.boy))
                    .into((ImageView) view);
                mSpinner.setVisibility(View.GONE);
                break;
            }
        }
        mSpinner.setVisibility(View.GONE);
    }
});

Usage Example

MyTeam.java
// In MyTeam activity - loading salesperson profile picture
final ImageView imageView = mView.findViewById(R.id.user_pic);
final ProgressBar spinnerImage = mView.findViewById(R.id.progressBar10);

ImageSetter.setImage(
    mView.getContext(),
    imageView,
    salesperson.getEmailId(),
    spinnerImage
);
AccountManager.java
// In AccountManager activity - loading current user's profile
ImageView profilePic = findViewById(R.id.profile_picture);
ProgressBar loadingSpinner = findViewById(R.id.image_spinner);

ImageSetter.setImage(
    getApplicationContext(),
    profilePic,
    currentUserEmail,
    loadingSpinner
);

Image Transformation

The method applies a circular crop transformation to all profile images using Glide’s RequestOptions.circleCropTransform(), creating a consistent circular avatar appearance throughout the app.

Placeholder

If no image is found or while loading, a default placeholder is shown:
  • Resource: R.mipmap.boy
  • Applied via: .placeholder(R.mipmap.boy)

Firebase Database Structure

The method queries the database path defined in Constants.DATABASE_UPLOADS:
Firebase Database
└── uploads/
    ├── upload1
    │   ├── email: "[email protected]"
    │   └── url: "https://firebase.storage.../profile.jpg"
    └── upload2
        ├── email: "[email protected]"
        └── url: "https://firebase.storage.../photo.jpg"

Dependencies

Glide

Image loading library with caching and transformations
implementation 'com.github.bumptech.glide:glide:4.8.0'

Firebase Database

Real-time database for storing image metadata
implementation 'com.google.firebase:firebase-database:16.0.1'

Performance Optimization

Before uploading to Firebase Storage, images are compressed to reduce storage costs and improve loading times. This compression happens in the upload flow within AccountManager.
Glide automatically caches loaded images, preventing redundant network requests when the same profile picture is displayed multiple times.

Error Handling

The method includes an onCancelled callback for database errors, though it doesn’t implement specific error handling. The ProgressBar is always hidden even if the image fails to load, preventing UI freezing.

Upload

Data model for image upload metadata

Constants

Contains DATABASE_UPLOADS constant for Firebase path

Source Reference

File: ~/workspace/source/app/src/main/java/project/avishkar/salesmanagement/ImageSetter.java

Build docs developers (and LLMs) love