将图片上传到Firebase时如何获取日期和时间?

时间:2019-02-05 04:54:28

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

我正在将图像上传/发送到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();
    }
}

1 个答案:

答案 0 :(得分:0)

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

您可以使用Firebase数据库服务器提供的ServerValue.TIMESTAMP并将其与每个图像的url和description字段一起存储在数据库的新timestamp字段中。执行写入操作时,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 PM

使用可以通过修改dateTimePattern字符串以任何所需的格式获取它,有关详细信息,请参见下面的链接。 https://developer.android.com/reference/java/text/SimpleDateFormat#date-and-time-patterns