Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

android - Can someone help me with logic of the firebase on success listener

I am adding and retrieving images from firebase database. When an image is uploaded, it is displayed on another activity in recyclerview. When adding multiple images, then activity keeps opening in the loop i.e dependent on how many images are added. I want to open all activities at once when all images are successfully added. I tried making a logic in which an int a is compared with the mClipData.getItemCount() but it doesn't give any solution kindly help me with this problem

P.S: the code given is for multiple images

    if (data.getClipData() != null) {
        ClipData mClipData = data.getClipData();
        ArrayList<Uri> mArrayUri = new ArrayList<>();
        for (int i = 0; i < mClipData.getItemCount(); i++) {

            final ClipData.Item item = mClipData.getItemAt(i);
            final Uri mImageUri = item.getUri();
            mArrayUri.add(mImageUri);
            // Get the cursor
            Cursor cursor = getContentResolver().query(mImageUri, filePathColumn, null, null, null);
            // Move to first row
            cursor.moveToFirst();

            final StorageReference profileStorage = uploadStorage.child(mImageUri.getLastPathSegment());
            uploadTask = profileStorage.putFile(mImageUri);
            final int finalI = i;
            uploadTask.addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    showMessage(e.toString());
                }
            }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    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();
                            }
                            downloadURL = profileStorage.getDownloadUrl().toString();
                            return profileStorage.getDownloadUrl();
                        }
                    }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                        @Override
                        public void onComplete(@NonNull Task<Uri> task) {
                            if(task.isSuccessful()){
                                Uri uri = task.getResult();
                                idRef.child(mImageUri.getLastPathSegment()).setValue(uri.toString());
                            }
                        }
                    }).addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            showMessage("image upload successful ");
                            // activity opens multiple times check it //
                            a++;
                        }
                    });
                }
            });

            if(a == mClipData.getItemCount()){
                Intent intent = new Intent(getApplicationContext(), DisplayImages.class);
                startActivity(intent);
            }

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            imageEncoded  = cursor.getString(columnIndex);
            imagesEncodedList.add(imageEncoded);
            cursor.close();
        }

    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...