如何上传图像时火力得到的日期和时间?

问题描述 投票:0回答:1

我上传/发送图像到存储火力和实时数据库。如何上传图像时获得的当前日期和时间?

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();
    }
}
firebase firebase-realtime-database firebase-storage
1个回答
0
投票

当我收到你的问题,你要存储当前的时间戳,以及当你存储在火力地堡实时数据库中的图像URL和说明。

您可以在数据库中对每个图像的URL和描述字段一起使用由火力地堡数据库服务器提供ServerValue.TIMESTAMP并将其存储在一个新的时间戳字段。在火力地堡服务器执行写操作时,写在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("");
          }
    });

这将创建一个键名“时间戳”为每个图像的新领域与代表如下面毫秒UTC时间戳的13位数字

{
    //...

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

    //....
}

虽然从数据库中读回,你可以在一个可变长整型说“戳”,它转换成日期如下时间得到这个值

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日星期一15:52

使用可以得到它在你通过修改dateTimePattern字符串喜欢的任何格式,请参阅下面的链接了解详情。 https://developer.android.com/reference/java/text/SimpleDateFormat#date-and-time-patterns

© www.soinside.com 2019 - 2024. All rights reserved.