Java Code Examples for com.google.firebase.storage.UploadTask#addOnCompleteListener()

The following examples show how to use com.google.firebase.storage.UploadTask#addOnCompleteListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProfileInteractor.java    From social-app-android with Apache License 2.0 6 votes vote down vote up
public void createOrUpdateProfileWithImage(final Profile profile, Uri imageUri, final OnProfileCreatedListener onProfileCreatedListener) {
    String imageTitle = ImageUtil.generateImageTitle(UploadImagePrefix.PROFILE, profile.getId());
    UploadTask uploadTask = databaseHelper.uploadImage(imageUri, imageTitle);

    if (uploadTask != null) {
        uploadTask.addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                Uri downloadUrl = task.getResult().getDownloadUrl();
                LogUtil.logDebug(TAG, "successful upload image, image url: " + String.valueOf(downloadUrl));

                profile.setPhotoUrl(downloadUrl.toString());
                createOrUpdateProfile(profile, onProfileCreatedListener);

            } else {
                onProfileCreatedListener.onProfileCreated(false);
                LogUtil.logDebug(TAG, "fail to upload image");
            }

        });
    } else {
        onProfileCreatedListener.onProfileCreated(false);
        LogUtil.logDebug(TAG, "fail to upload image");
    }
}
 
Example 2
Source File: ProfileInteractor.java    From social-app-android with Apache License 2.0 5 votes vote down vote up
public void createOrUpdateProfileWithImage(final Profile profile, Uri imageUri, final OnProfileCreatedListener onProfileCreatedListener) {
    String imageTitle = ImageUtil.generateImageTitle(UploadImagePrefix.PROFILE, profile.getId());
    UploadTask uploadTask = databaseHelper.uploadImage(imageUri, imageTitle);

    if (uploadTask != null) {
        uploadTask.addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                task.getResult().getStorage().getDownloadUrl().addOnCompleteListener(task1 -> {
                    if (task1.isSuccessful()) {
                        Uri downloadUrl = task1.getResult();
                        LogUtil.logDebug(TAG, "successful upload image, image url: " + String.valueOf(downloadUrl));

                        profile.setPhotoUrl(downloadUrl.toString());
                        createOrUpdateProfile(profile, onProfileCreatedListener);
                    } else {
                        onProfileCreatedListener.onProfileCreated(false);
                        LogUtil.logDebug(TAG, "createOrUpdateProfileWithImage, fail to getDownloadUrl");
                    }
                });
            } else {
                onProfileCreatedListener.onProfileCreated(false);
                LogUtil.logDebug(TAG, "createOrUpdateProfileWithImage, fail to upload image");
            }

        });
    } else {
        onProfileCreatedListener.onProfileCreated(false);
        LogUtil.logDebug(TAG, "fail to upload image");
    }
}
 
Example 3
Source File: FirebaseStorageHelper.java    From NaviBee with GNU General Public License v3.0 4 votes vote down vote up
public static void uploadImage(Uri uri, String uploadName, String category,
          int compressQuality, boolean thumbOnly, UploadCallback callback) throws IOException {


    // storage reference
    FirebaseStorage storage = FirebaseStorage.getInstance();
    StorageReference storageRef = storage.getReference();
    storageRef = storageRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child(category);

    if (uploadName==null) {
        uploadName = UUID.randomUUID().toString();
    }

    StorageReference storageRefOri = storageRef.child(uploadName + ".jpg");
    StorageReference storageRefThumb = storageRef.child(uploadName + "-thumb.jpg");

    String fullFilename = storageRefOri.getPath();




    ArrayList<UploadTask> tasks = new ArrayList<>();


    if (!thumbOnly) {
        // image
        byte[] bo = scaleImage(uri, IMAGE_SIZE, compressQuality);
        tasks.add(storageRefOri.putBytes(bo));
    }

    // thumb
    byte[] bt = scaleImage(uri, THUMB_IMAGE_SIZE, compressQuality);
    tasks.add(storageRefThumb.putBytes(bt));

    AtomicBoolean failed = new AtomicBoolean(false);

    for (UploadTask task : tasks) {
        task.addOnCompleteListener(result -> {
            if (failed.get()) return;

            if (result.isSuccessful()) {
                tasks.remove(task);
                if (tasks.isEmpty()) {
                    callback.callback(true, fullFilename);
                }
            } else {
                failed.set(true);
                for (UploadTask t:tasks) {
                    if (t!=task) {
                        t.cancel();
                    }
                }
                callback.callback(false, "");
            }
        });
    }
}