[英]Getting time of upload from firebase storage
我需要顯示我的應用程序上每張照片的上傳時間,我正在考慮使用 Firebase Storage 中的元數據。 我可以通過這種方式獲得我需要的時間和日期,但僅限於特定圖像,如何為我的所有圖像獲取它並將其顯示在適當的位置,而無需在我的模型類中使用它? (不確定如何將它包含在我的模型類中,因為我沒有將數據保存在 firebase 數據庫中,我只是直接從 Storage 中獲取它)。
這是我到目前為止所做的,以及如何獲取一張圖像的時間和日期:
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());
}
});
}
}
在 String 的幫助下提供路徑,以便稍后使用。 確保您提供正確的路徑。
String path = "Posts/"+System.currentTimeMillis()+ "." + getFileExtension(mImageUri)
StorageReference fileReference = mStorageRef.child(path);
正如您可以從 存儲指南中閱讀的那樣 ,您可以借助存儲位置獲取元數據。 然后將路徑存儲在 Firebase DB 中。 每次嘗試檢索文件時,您都可以訪問它的元數據。
upload = new Posts(path, downloadUrl.toString(), mEditTextFileName.getText().toString().trim(), mAuth.getUid());
讓您知道您需要在數據模型中進行哪些更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.