我基本上是想通过在最后一个点上分割字符串来捕获文件扩展名。但是有时候文件没有任何扩展名,所以我预料到这种情况。
但问题是一些文件名在结尾前有点号,像这样...
但问题是一些文件名在结尾前有点号,像这样...
/mnt/sdcard/OG Ron C, Chopstars & Drake - Choppin Ain't The Same-2013-MIXFIEND/02 Drake - Connect (Feat. Fat Pat) (Chopped Not Slopped).mp3
所以当这个字符串出现时,它会在“02 Drake - Connect (Feat.”处截断。
这是我一直在使用的...
String filePath = intent.getStringExtra(ARG_FILE_PATH);
String fileType = filePath.substring(filePath.length() - 4);
String FileExt = null;
try {
StringTokenizer tokens = new StringTokenizer(filePath, ".");
String first = tokens.nextToken();
FileExt = tokens.nextToken();
}
catch(NoSuchElementException e) {
customToast("the scene you chose, has no extension :(");
}
System.out.println("EXT " + FileExt);
File fileToUpload = new File(filePath);
如何在文件扩展名处拆分字符串,同时也能处理并警报没有扩展名的文件。
lastIndexOf()
函数,结合一些启发式方法来判断它是表示文件名的一部分还是表示文件扩展名。 - laalto