使用firebase上传图片。无法获得上传的进度

时间:2016-07-14 02:01:40

标签: android firebase firebase-storage

我输入代码作为firebase存储结果的文档只有0.0

uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
        double progress = 100.0* (taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());

        dialog.setMessage("uploading "+progress+" %");
        dialog.show();
    }
});

1 个答案:

答案 0 :(得分:0)

这就是你可以将图像上传到Firebase存储的好运;)

 private void uploadFromUri(Uri fileUri) {
        Log.d(TAG, "uploadFromUri:src:" + fileUri.toString());

        // [START get_child_ref]
        // Get a reference to store file at photos/<FILENAME>.jpg
        final  StorageReference photoRef = storageRef.child("images")
                .child(fileUri.getLastPathSegment());
        // [END get_child_ref]

        // Upload file to Firebase Storage
        // [START_EXCLUDE]
        showProgressDialog();
        // [END_EXCLUDE]
        Log.d(TAG, "uploadFromUri:dst:" + photoRef.getPath());
        photoRef.putFile(fileUri)
                .addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        // Upload succeeded
                        Log.d(TAG, "uploadFromUri:onSuccess");

                        // Get the public download URL
                        mDownloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
                        Log.d( "The Download Url", mDownloadUrl.toString());


                        // [START_EXCLUDE]
                        hideProgressDialog();
                     //   updateUI(mAuth.getCurrentUser());
                        // [END_EXCLUDE]
                    }
                })
                .addOnFailureListener(this, new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {
                        // Upload failed
                        Log.w(TAG, "uploadFromUri:onFailure", exception);

                        mDownloadUrl = null;

                        // [START_EXCLUDE]
                        hideProgressDialog();
                        Toast.makeText(NewProduct.this, "Error: upload failed",
                                Toast.LENGTH_SHORT).show();
                       // updateUI(mAuth.getCurrentUser());
                        // [END_EXCLUDE]
                    }
                });
    }
    // [END upload_from_uri]