简体   繁体   English

Android-检查文件扩展名是否为.mp4

[英]Android - Check if file extension is .mp4

I want to check if the user entered the extension .mp4 and if it was entered correctly. 我想检查用户是否输入了扩展名.mp4以及输入是否正确。 I know I can do something like this: 我知道我可以做这样的事情:

if (Rename[0].indexOf(".") > 0) {

}

But the problem with this is that it just checks if there is more than one character entered, so if .mp5 has been entered or any incorrect extension, it would be accepted. 但是,这样做的问题在于,它仅检查是否输入了多个字符,因此,如果输入了.mp5或任何不正确的扩展名,它将被接受。

here is the context in which I want to use it: 这是我要使用它的上下文:

alertDialogBuilder.setCancelable(false)
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            Rename[0] = editText.getEditableText().toString();

            if (Rename[0] != null){

            //This is where I want to check if the extension has been correctly entered

            }
}

So my question is: 所以我的问题是:

How can I get the text from Rename{0} , this has already been done, and check if the file extension .mp4 has been entered correctly, if it has not, then add it, else if it was entered (correctly) ignore it? 我如何Rename{0}获取文本,这已经完成,然后检查是否正确输入了文件扩展名.mp4 ,如果没有输入,则将其添加,否则(如果正确输入)将其忽略?

Try this 尝试这个

if (Rename[0].endsWith(".mp4")) {

        }

do this by below code : 通过下面的代码做到这一点:

alertDialogBuilder.setCancelable(false)
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            Rename[0] = editText.getEditableText().toString();

            if (Rename[0] != null){

              if(Rename[0].contains(".mp4"){
             //do what you want to do here
             }

            }
}

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

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