Android method for obtaining various types of local files

introduce

This article introduces the method for Android to obtain various types of files on the machine, which has been encapsulated into a tool class, with the source code download address at the end.

Tips

To obtain music, video, pictures, documents and other files, you need to have the permission to read the SD card. If it is a system below 6.0, you can directly declare the SD card reading permission in the list file; If it is 6.0 or above, you need to dynamically apply for permission.

Use of FileManager

FileManager is a encapsulated tool class used to obtain various local files. For example, FileManager. GetInstance (context context). Getmusic() is created in singleton mode:

Get music list

The method isexists (string path) in FileUtils to determine whether a file exists is as follows:

The bean class music code of music is:

Pinyinutils obtains Pinyin by name, which is mainly used for sorting music lists A-Z. pinyin4j.jar needs to be relied on. The method to obtain pinyin is getpinyin (string name) code:

Get video list

The bean class video code of video is:

Obtain video thumbnail through local video ID

In the above method to obtain the video list, an attribute in the video object is ID, and the bitmap object of the video thumbnail can be obtained by passing in this ID.

Get all picture folders on this computer

The bean class imgfolderbean code of the picture folder is:

Gets the collection of picture paths under the picture folder

Get the list of installed apps on this computer

The bean class appinfo code of the installation package information is:

Obtain documents, compressed packages, APK installation packages, etc

The incoming filetype file type is the file type declaration defined in FileUtils:

Where, the method getfiletype (string path) of FileUtils to obtain the file type according to the file path is:

The bean class filebean code of the file is:

FileUtils is a method to obtain the image resource ID according to the file type. The getfileiconbypath (path) code is:

The pictures of the above file types are placed in mipmap for display when displaying the file list.

For the source code of FileManager and other classes, you can click the following website to jump to view and download:

Click to view the source code (the file in the phone directory)

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>