Android--取出SDcard卡上指定后缀名的文件
1.
import java.io.File; import java.io.FilenameFilter; /** * 后缀名过滤器 * @author ZLQ * */ public class FileNameSelector implements FilenameFilter{ String extension = "."; public FileNameSelector(String fileExtensionNoDot) { extension += fileExtensionNoDot; } public boolean accept(File dir, String name) { return name.endsWith(extension); } }
2.
public class AndroidSdCardActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); getName(); } /** * 得到以mp3结尾的文件 */ public void getName(){ File file = Environment.getExternalStorageDirectory(); if(file != null){ File[] files = file.listFiles(new FileNameSelector("mp3")); for(int i = 0; i < files.length; ++i){ Log.i("System.out", files[i].getName()); } } } }
时间: 2024-10-09 17:46:59