简体   繁体   English

从 Firebase 存储获取上传时间

[英]Getting time of upload from firebase storage

I need to display the time each photo on my app was uploaded, and I was thinking of using the metadata from Firebase Storage.我需要显示我的应用程序上每张照片的上传时间,我正在考虑使用 Firebase Storage 中的元数据。 I can get the time and date that I need that way, but only for specific images, how do I get it for all my images and display it in the appropriate places, without having it in my model class?我可以通过这种方式获得我需要的时间和日期,但仅限于特定图像,如何为我的所有图像获取它并将其显示在适当的位置,而无需在我的模型类中使用它? (Not sure how to include it in my model class, since I'm not saving the data in firebase Database, I'm just getting it directly from Storage). (不确定如何将它包含在我的模型类中,因为我没有将数据保存在 firebase 数据库中,我只是直接从 Storage 中获取它)。

Here is what i've done so far, and how i can get the time and date for one image:这是我到目前为止所做的,以及如何获取一张图像的时间和日期:

 storageRef = FirebaseStorage.getInstance().getReference().child("Posts/1577360878055.jpg");

.. ..

        holder.storageRef.getMetadata().addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {
        @Override
        public void onSuccess(StorageMetadata storageMetadata) {

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(storageMetadata.getCreationTimeMillis());

            int mYear = calendar.get(Calendar.YEAR);
            int mMonth = calendar.get(Calendar.MONTH);
            int mDay = calendar.get(Calendar.DAY_OF_MONTH);
            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            int min = calendar.get(Calendar.MINUTE);

            Log.e("metadata",""+calendar.get(Calendar.DAY_OF_MONTH)+"/"+calendar.get(Calendar.MONTH)+"/"+calendar.get(Calendar.YEAR)+" :: "+calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE));



            holder.t_counter.setText(calendar.get(Calendar.DAY_OF_MONTH)+"/"+calendar.get(Calendar.MONTH)+"/"+calendar.get(Calendar.YEAR));
        }

    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Uh-oh, an error occurred!
        }
    });

Do I have to first transfer the date to Firebase Database and then read it in my code through the model class?我是否必须先将日期传输到 Firebase 数据库,然后通过模型类在我的代码中读取它? If, yes, how do I do that?如果是,我该怎么做? How can I transfer my date metadata to Firebase Database?如何将我的日期元数据传输到 Firebase 数据库? I can also show the code for the part where I'm sending the Image URI from Storage to Database if that'll be necessary.如果有必要,我还可以显示将图像 URI 从存储发送到数据库的部分的代码。

Code for Uploading data to database:上传数据到数据库的代码:

    //get image URI
private String getFileExtension(Uri uri) {
    ContentResolver cR = getContentResolver();
    MimeTypeMap mime = MimeTypeMap.getSingleton();
    return mime.getExtensionFromMimeType(cR.getType(uri));
}

//function to upload image to Firebase storage and send URL to Database
private void uploadFile() {
    if (mImageUri == null ) {
        Toast.makeText(this, "No image selected", Toast.LENGTH_LONG).show();
    } else {
        StorageReference fileReference = mStorageRef.child(System.currentTimeMillis()
                + "." + getFileExtension(mImageUri));

        mUploadTask = fileReference.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                Toast.makeText(AdminUploadActivity.this, "Upload successful", Toast.LENGTH_LONG).show();

                //get image url from storage
                Task<Uri> urlTask = taskSnapshot.getStorage().getDownloadUrl();
                while (!urlTask.isSuccessful()) ;
                Uri downloadUrl = urlTask.getResult();

                Posts upload;

                //call the constructor from the Posts class in "models" package
                upload = new Posts(downloadUrl.toString(), mEditTextFileName.getText().toString().trim(), mAuth.getUid());


                String uploadId = mDatabaseRef.push().getKey();
                mDatabaseRef.child(uploadId).setValue(upload);



            }
        })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(AdminUploadActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                })
                .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                        double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
                    }
                });

    }
}

Provide the path with help of String so you use it later.在 String 的帮助下提供路径,以便稍后使用。 Make sure your providing proper path.确保您提供正确的路径。

String path = "Posts/"+System.currentTimeMillis()+ "." + getFileExtension(mImageUri)

StorageReference fileReference = mStorageRef.child(path);

As you can read from the Guide of Storage You can get metadata with the help of storage location.正如您可以从 存储指南中阅读的那样 您可以借助存储位置获取元数据。 Then store the path inside Firebase DB.然后将路径存储在 Firebase DB 中。 Each time you try to retrieve the file you can access it's meta data.每次尝试检索文件时,您都可以访问它的元数据。

upload = new Posts(path, downloadUrl.toString(), mEditTextFileName.getText().toString().trim(), mAuth.getUid());

Rest you know what changes you need in your data model.让您知道您需要在数据模型中进行哪些更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM