【问题标题】:How to get the date and time when uploading image to firebase?将图像上传到firebase时如何获取日期和时间?
【发布时间】:2019-06-28 21:15:35
【问题描述】:

我正在将图像上传/发送到 Firebase 存储和实时数据库。上传图片时如何获取当前日期和时间?

private void uploadImage() {
    if (imgUrl != null) {
        final StorageReference fileReference = mStorageRef.child(System.currentTimeMillis() + "." + getFileExtension(imgUrl));

        mUploadTask = fileReference.putFile(imgUrl)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        Calendar calendar = Calendar.getInstance();

                        final String currentdate = DateFormat.getDateInstance().format(calendar.getTime());
                        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
                        String time = format.format(calendar.getTime());
                        Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                uploadProgress.setProgress(0);
                            }
                        }, 500);
                        fileReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                            @Override
                            public void onSuccess(Uri uri) {
                                Notify upload = new Notify(imgDescription.getText().toString().trim(), uri.toString());
                                String uploadID = mDatabaseRef.push().getKey();
                                mDatabaseRef.child(uploadID).setValue(upload);
                                Toast.makeText(StudentNotify.this, "Upload successfully", Toast.LENGTH_LONG).show();
                                imgDescription.setText("");
                            }
                        });


                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(StudentNotify.this, e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                })
                .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                        double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
                        uploadProgress.setProgress((int) progress);
                    }
                });
    } else {
        Toast.makeText(StudentNotify.this, "No file selected", Toast.LENGTH_SHORT).show();
    }
}

【问题讨论】:

  • 我不知道你在问什么。你写的代码有什么问题?
  • 我想把当前的日期和时间连同图片的url一起发送到firebase实时数据库。

标签: firebase firebase-realtime-database firebase-storage


【解决方案1】:

当我收到您的问题时,您希望在将图像 url 和描述存储在 Firebase 实时数据库中时也存储当前时间戳。

您可以使用 Firebase 数据库服务器提供的 ServerValue.TIMESTAMP 并将其与每个图像的 url 和描述字段一起存储在数据库中的新时间戳字段中。 Firebase 服务器在执行写入时以 UTC 毫秒为单位写入当前时间戳来代替此值。此值为 13 位数值。 https://firebase.google.com/docs/reference/android/com/google/firebase/database/ServerValue

包括下面的代码来为每张图片写这个值

    fileReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
          @Override
          public void onSuccess(Uri uri) {
              Notify upload = new Notify(imgDescription.getText().toString().trim(), 
                                         uri.toString());
              String uploadID = mDatabaseRef.push().getKey();
              mDatabaseRef.child(uploadID).setValue(upload);

              mDatabaseRef.child(uploadID).child("timestamp").setValue(ServerValue.TIMESTAMP);

              Toast.makeText(StudentNotify.this, "Upload successfully", Toast.LENGTH_LONG).show();
              imgDescription.setText("");
          }
    });

这将为每个图像创建一个键名为“timestamp”的新字段,其中一个 13 位数字表示 UTC 时间戳,以毫秒为单位,如下所示

{
    //...

    imagePushKeyId1: {
        url: "image url here",
        desc: "image description here"
        timestamp: 1549347429000
    }

    //....
}

在从数据库中读回时,您可以在变量“timestamp”中将此值作为长整数获取,并将其转换为日期和时间,如下所示

Date date = new Date(timestamp);
String dateTimePattern = "MMM dd, yyyy EEE h:mm a";
String dateText = new SimpleDateFormat(dateTimePattern).format(date);

它以下面的格式给出 dateText 2019 年 1 月 28 日星期一下午 3:52

使用可以通过修改 dateTimePattern 字符串来获取你喜欢的任何格式,详情参考下面的链接。 https://developer.android.com/reference/java/text/SimpleDateFormat#date-and-time-patterns

【讨论】:

  • 谢谢。有效。如何从时间戳中检索日期和时间?
  • 太好了,您可以先将时间戳转换为 Date 对象,然后使用带有模式的 SimpleDateFormat 以获取所需格式的日期和时间。我已经使用示例模式编辑了答案,该模式以 2019 年 1 月 28 日星期一下午 3:52 的格式给出了 dateText。
  • @Vishnu 如果您对帮助该社区的其他人感到满意,请不要忘记将答案标记为已接受 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-28
  • 2017-11-24
  • 2021-10-12
相关资源
最近更新 更多