[英]How to check the filetype of an uploaded file on Cloud Storage for Firebase?
I've got a simple file upload system in my app that uploads a .mp3
file to firebase.我的应用程序中有一个简单的文件上传系统,可以将
.mp3
文件上传到 firebase。 I would like to check to make sure the file being uploaded is definitely a .mp3
file and is no bigger than 80MB.我想检查以确保上传的文件绝对是
.mp3
文件并且不大于 80MB。 Is there a way to do this?有没有办法做到这一点?
Here is my code for uploading the files, at the moment:这是我目前上传文件的代码:
private void getMixForUpload() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("mp3/*");
Intent chooser = Intent.createChooser(intent, "Choose Your Mix");
startActivityForResult(chooser, ACTION_REQUEST_GALLERY);
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == ACTION_REQUEST_GALLERY && resultCode == RESULT_OK) {
Uri uri = data.getData();
StorageReference filepath = FirebaseStorage.getInstance().getReference().child("DjMixes").child(uri.getLastPathSegment());
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
taskSnapshot.getMetadata().getReference().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Uri downloadUrl = uri;
firebaselogourl = downloadUrl.toString();
}
});
}
});
}
So, basically, you have to use File Metadata .所以,基本上,你必须使用File Metadata 。 Considering that you are getting the reference right and everything about the upload process is right, here is the way to do what you want:
考虑到您获得了正确的参考并且有关上传过程的所有内容都是正确的,这是执行您想要的操作的方法:
taskSnapshot.getMetadata().addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {
@Override
public void onSuccess(StorageMetadata storageMetadata) {
long sizeMB = storageMetadata.getSizeBytes() / (1024 * 1024);
String fileType = storageMetadata.getContentType();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle your error, something bad happened :(
}
});
Now that you have the size (in megabytes) and the content type you can just use a if
statement like this:现在您有了大小(以兆字节为单位)和内容类型,您可以使用这样的
if
语句:
if (fileType.matches("audio/mpeg") && sizeMB < 80)) {
//your file is a audio file congratulations
//your file also is bellow 80mb, good :)
} else {
//the above statement is not true
}
As the OP pointed out it's much better to use the .matches()
function, which, in this instance, is the only one that works.正如 OP 指出的那样,最好使用
.matches()
function,在这种情况下,它是唯一有效的。
NOTE: If you planning on deleting the file from firebase storage in case it's bigger than 80MB or it's not an mp3
file, I suggest that you do these things before uploading the files.注意:如果您打算从 firebase 存储中删除大于 80MB 或不是
mp3
文件的文件,我建议您在上传文件之前执行这些操作。 Basically, check if your file is mp3
and it's smaller than 80MB, then go upload it.基本上,检查您的文件是否为
mp3
且小于 80MB,然后 go 上传。 It improves the flow of the program and it's, frankly, a better way to do it anyways.它改善了程序的流程,坦率地说,无论如何,它是一种更好的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.