从Firebase存储获取上传时间

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

我需要显示应用程序中每张照片的上传时间,并且我正在考虑使用Firebase Storage中的元数据。我可以获取所需的时间和日期,但仅针对特定图像,如何获取所有图像并将其显示在适当的位置,而不必将其放在模型类中? (不知道如何将其包括在模型类中,因为我没有将数据保存在firebase数据库中,所以我只是直接从存储中获取数据。)>

这里是我到目前为止所做的,以及如何获取一张图像的时间和日期:

 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!
        }
    });

我必须先将日期传输到Firebase数据库,然后通过模型类在代码中读取它吗?如果是,我该怎么做?如何将日期元数据传输到Firebase数据库?如果需要的话,我还可以显示将图像URI从存储发送到数据库的那部分代码。

用于将数据上传到数据库的代码:

    //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());
                    }
                });

    }
}

我需要显示应用程序中每张照片的上传时间,并且我正在考虑使用Firebase Storage中的元数据。我可以获取需要的时间和日期,但仅适用于特定的...

java android firebase firebase-storage
1个回答
1
投票

在String的帮助下提供路径,以便以后使用。确保您提供正确的路径。

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