簡體   English   中英

活動銷毀后,應用會繼續上傳到Firebase存儲

[英]App keep uploading to Firebase Storage when activity is destroyed

單擊按鈕后,我需要將圖像列表上傳到Firebase存儲,然后銷毀該活動,而無需用戶采取任何其他操作。

單擊按鈕后,我下面的代碼立即破壞活動,並讓上傳進度仍在后台進行。

我想要的是在等待上載進度首先完成之后破壞活動。

我應該怎么做才能解決這個問題?

// on button click
public void send(View view) {

        imageCount = 0;
        imageKey = databaseReference.push().getKey();

        uploadFile( bitmaps );

        intent.putExtra( "location", location);
        intent.putExtra( "description", description);
        intent.putExtra("date", date);
        setResult( 1, intent );

        finish();
    }


private void uploadFile(final ArrayList<Bitmap> bitmaps) {

        FirebaseStorage storage = FirebaseStorage.getInstance();

        for (final Bitmap bitmap : bitmaps) {

            final StorageReference imageRef = storage
                    .getReferenceFromUrl( "gs://example.appspot.com/" )
                    .child( folderName + "/" + UUID.randomUUID() + ".jpeg" );

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress( Bitmap.CompressFormat.JPEG, 20, baos );
            byte[] data = baos.toByteArray();

            UploadTask uploadTask = imageRef.putBytes( data );

            uploadTask.addOnFailureListener( new OnFailureListener() {

                @Override
                public void onFailure(@NonNull Exception exception) {

                    Toast.makeText( LaporActivity.this, "Fail", Toast.LENGTH_SHORT ).show();

                }
            } ).addOnSuccessListener( new OnSuccessListener<UploadTask.TaskSnapshot>() {

                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                    imageRef.getDownloadUrl().addOnSuccessListener( new OnSuccessListener<Uri>() {

                        @Override
                        public void onSuccess(Uri uri) {

                            databaseReference.child( "images" ).child( imageKey ).child( Integer.toString( imageCount ) ).setValue( uri.toString() );

                            imageCount++;

                        }
                    } );
                }
            } );
        }
    }

上傳完成后,只需在回調中調用finish()即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM