将图像上传到Firebase时显示上载百分比

时间:2017-02-05 13:17:30

标签: android firebase firebase-storage

我正在尝试将图片上传到Firebase,并希望在上传期间看到上传百分比....这里我的代码: -

StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("Chat_Images");
StorageReference filepath = storageReference.child(ImageURI.getLastPathSegment());

final String path = filepath.getPath();


filepath.putFile(ImageURI).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {

        double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
        //System.out.println("Upload is " + progress + "% done");
        int currentprogress = (int) progress;
        Toast.makeText(CHATmainACTIVITY.this, "Upload "+currentprogress, Toast.LENGTH_SHORT).show();

    }
}).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
        TastyToast.makeText(CHATmainACTIVITY.this, "Upload Has Stopped Due to Network Error..", TastyToast.LENGTH_SHORT,TastyToast.ERROR).show();
    }
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        DatabaseReference message_roottemp = imageref.child(temp_key);
        String downloadURI2= taskSnapshot.getDownloadUrl().toString();
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("Text", "");
        map2.put("sender_name", current_user.replace("@zumi.com", ""));
        map2.put("sender_photo", downloadURI2);
        map2.put("timestamp", mydate);
        map2.put("message_status", "Saved");
        map2.put("msg_type", "IMAGE");
        message_roottemp.updateChildren(map2);
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        TastyToast.makeText(CHATmainACTIVITY.this, "Upload Has Stopped Due to Network Error..", TastyToast.LENGTH_SHORT,TastyToast.ERROR).show();

    }
});

代码在Toast中显示0然后直接100%......

0 个答案:

没有答案