Home > OS >  Android: how to finish the activity after all Uploads finish
Android: how to finish the activity after all Uploads finish

Time:12-02

So i am using for loop to upload multiple images to firebase Storage, now i want to make the Activity closed after all Images are uploaded

i tried to use finish(); but cuz it's a loop so finish would be called from first loop;

for (uploadCount  = 0; uploadCount < ImageList.size(); uploadCount  ) {
        
    imagePath = productRef.child(UID).child("images").push().getKey();
    Uri IndividualImage = ImageList.get(uploadCount);
    StorageReference ImageName = productImagesRef.child(UID).child(imagePath);

    //Compress Images
    Bitmap bmp = null;
    try {
        bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), IndividualImage);
    } catch (IOException e) {
        e.printStackTrace();
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 25, baos);
    byte[] data = baos.toByteArray();
    //End of compressing

    //start on uploading compressed
    ImageName.putBytes(data).addOnSuccessListener(taskSnapshot -> ImageName.getDownloadUrl()
            .addOnSuccessListener(uri -> {
        String url = String.valueOf(uri);
        StoreLink(url);
    }));
}

this the firebase realtime upload:

private void StoreLink(String url) {
    HashMap<String ,Object> hashMap = new HashMap<>();
    hashMap.put("image", url);
    hashMap.put("id", imagePath);
    dialog.dismiss();

    assert imagePath != null;
    productRef.child(UID).child("images").child(imagePath).updateChildren(hashMap);
}

CodePudding user response:

You can declare a global variable :

int countLoop = 0;

and then inside the storeLink() you can increase : countLoop ;

and onSuccessListener() use if to check if countLoop == Imagelist.size();

would be something like that :

    private void StoreLink(String url) {
        HashMap<String ,Object> hashMap = new HashMap<>();
        hashMap.put("image", url);
        hashMap.put("id", imagePath);
        dialog.dismiss();

        countLoop   ;
    
        assert imagePath != null;
        productRef.child(ownerUid.getText().toString()).child(Constants.KEY_APARTMENTS)
                    .child(uid_et).child(Constants.KEY_PHASES).child(path).updateChildren(hashMap2).addOnCompleteListener(task -> {
                        if (task.isComplete()) {
                            //////////////////////////////////
                            if (countLoop == ImageList.size()) {
                                loadingDialog.dismiss();
                                finish();
                            }
    
                        } else {
                            loadingDialog.dismiss();
                            String errorMessage = Objects.requireNonNull(task.getException()).toString();
                            showToast("Error: "   errorMessage);
                        }
                    });

}
  • Related