简体   繁体   中英

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. 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).

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? If, yes, how do I do that? How can I transfer my date metadata to Firebase Database? 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.

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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