Sergio Sergio - 3 months ago 101x
Android Question

Android Firebase upload file time

Hi I am implementing the storage upload in firebase for images.

What I am doing is select an image from my gallery, then display it on a ImageView and then upload it to the storage.

Everything works. But my concern is the first time I upload an image, I have tested and takes around a minute to return the download path.

After the first time takes less than a second. My problem is that I want to use that path for another ImageViews after closing the "upload image" activity.

Here is my code.

I have a dialog to select the gallery(I will have the facebook option later)

private View.OnClickListener dialogListener = new View.OnClickListener() {

public void onClick(View view) { clicked.length());
final Dialog dialog = new Dialog(EditProfileActivity.this);
dialog.setTitle("Gallery Options");

Button gallery = (Button) dialog.findViewById(;
gallery.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);

Then here is the Activity Result

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
Log.d(TAG, "PICTURE PATH IN PHONE: " + picturePath);
clicked = "iv_"+clicked;
int resID = getResources().getIdentifier(clicked, "id", "");
ImageView imageView = (ImageView) findViewById(resID);

Uri file = Uri. fromFile(new File(picturePath));
String extension = file.getLastPathSegment();
extension = extension.substring(extension.lastIndexOf("."));
StorageReference riversRef = storageRef.child("images/"+ user.getUid().toString()+"/"+clicked+extension);
UploadTask uploadTask = riversRef.putFile(file);

// Register observers to listen for when the download is done or if it fails
uploadTask.addOnFailureListener(new OnFailureListener() {
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
Uri downloadUrl = taskSnapshot.getDownloadUrl();
Log.d(TAG, "DOWNLOAD PATH IN FIREBASE: " + downloadUrl);


The time between both logs "PATH IN PHONE" and "PATH IN FIREBASE" is around a minute the first time, then is quite fast. The question is, do you know a way or method or pattern to have the speed upload all the time and not just the second time?
Or what do you suggest? is it better store the image in Base64 using the realtime database instead of storage?


The reason you see the lag on your first upload is because Firebase is backed by Google App Engine which when your backend is not in use for a long period of time basically puts it in a paused state to save resources.

When you go to make a request when it is in this paused state it needs to spin back up everything which is when you see the lag in upload time