如何使用特定格式的文件访问SD卡和返回和数组?

4 android

我需要访问SD卡并返回一些不同格式的文件.该位置将由用户输入.我该如何以编程方式执行此操作?

Epi*_*aos 6

Simondid,我相信这就是你要找的东西.

访问SDCard: 从android中的sdcard 读取特定文件

请记住检查媒体可用性: http ://developer.android.com/guide/topics/data/data-storage.html#filesExternal

创建文件过滤器: http ://www.devdaily.com/blog/post/java/how-implement-java-filefilter-list-files-directory

mp3文件过滤器示例,创建以下过滤器类:

import java.io.*;

/**
 * A class that implements the Java FileFilter interface.
 * It will filter and grab only mp3
 */
public class Mp3FileFilter implements FileFilter
{
  private final String[] okFileExtensions = 
    new String[] {"mp3"};

  public boolean accept(File file)
  {
    for (String extension : okFileExtensions)
    {
      if (file.getName().toLowerCase().endsWith(extension))
      {
        return true;
      }
    }
    return false;
  }
}
Run Code Online (Sandbox Code Playgroud)

然后基于访问SD卡的早期帖子,您将使用这样的过滤器:

File sdcard = Environment.getExternalStorageDirectory();
File dir = new File(sdcard, "path/to/the/directory/with/mp3");

//THIS IS YOUR LIST OF MP3's
File[] mp3List = dir.listFiles(new Mp3FileFilter());
Run Code Online (Sandbox Code Playgroud)

注意:代码很粗糙,您可能希望确保SD卡可用,如上所述