【问题标题】:uploading image with firebase . cant get the progress of uploading [duplicate]使用 firebase 上传图片。无法获取上传进度[重复]
【发布时间】:2016-07-14 02:01:40
【问题描述】:

我键入代码作为 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();
    }
});

【问题讨论】:

  • 代码示例有错误。见stackoverflow.com/questions/38278249/…
  • 不用谢我。我是在文档中编写损坏代码示例的人。 ;-) 感谢@qbix 的那个(通过支持他的回答)。
  • 你也可以从我的博客试试:engineering.letsnurture.com/firebase-storage
  • taskSnapshot.getTotalByteCount() 得到 -1。这可能是什么问题。我知道数据是以 256k 的块上传的。但是为什么要传输的所有字节的总和为负值?

标签: android firebase firebase-storage


【解决方案1】:

这是您可以将图像上传到 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]

【讨论】:

    猜你喜欢
    • 2021-01-24
    • 1970-01-01
    • 2021-03-19
    • 2021-01-13
    • 2018-06-14
    • 2021-08-18
    • 1970-01-01
    • 2023-03-06
    相关资源
    最近更新 更多