如何在Android上检查文件扩展名 [英] How to check file extension on Android

查看:106
本文介绍了如何在Android上检查文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,当我们从SD卡浏览文件时,这些文件将是.text,.jpg和mpeg4ie视频文件.

In my application, when we browse the file from the SD card, the files will be .text, .jpg and mpeg4ie video files.

我想将每种文件类型存储到一个特定的文件夹中.例如,.text文件转到文本文件夹.选择文件后,如何检查文件扩展名?

I want to store each file type into a particular folder. For example, .text files go to the text folder. When I select the file, how do I check the file extension?

推荐答案

我将以字符串形式获取文件名,并使用."将其拆分为一个数组.作为定界符,然后获取数组的最后一个索引,即文件扩展名.例如:

I would get the file name as a String, split it into an array with "." as the delimiter, and then get the last index of the array, which would be the file extension. For example:

public class main {
    public static void main(String[] args) {
        String filename = "image.jpg";
        String filenameArray[] = filename.split("\\.");
        String extension = filenameArray[filenameArray.length-1];
        System.out.println(extension);
    }
}

哪个输出:

jpg

这篇关于如何在Android上检查文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆