美文网首页
Android 读取SD卡指定后缀的文件

Android 读取SD卡指定后缀的文件

作者: 苏墨白梦觉泪满青衫湿 | 来源:发表于2017-03-14 10:20 被阅读0次

    一、读取SD卡指定后缀的所有文件

    在实际开发过程中遇到了这样的需求, 要读取sd卡上所有的gif文件, 包括微信,微博等等保存下来的,实际上原理和删除文件差不多, 递归的调用删除:

    MainActivity

    public class MainActivity extends AppCompatActivity implements Handler.Callback{    private ImageView imageView;    private ListView listView;    private Adapters adapters;    private List> list = new ArrayList<>();    private ListfileList = new ArrayList<>();    private Handler handler = new Handler(this);    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        final File path = Environment.getExternalStorageDirectory();// 获得SD卡路径;        File[] files = path.listFiles();// 读取        final String URLpath = path.getAbsolutePath();        listView = (ListView) findViewById(R.id.listView);        imageView = (ImageView) findViewById(R.id.imageView);        imageView.setVisibility(View.VISIBLE);        adapters = new Adapters(this,list);        listView.setAdapter(adapters);        new Thread(new Runnable() {            @Override            public void run() {                    getSuffixFile(fileList,URLpath,"mp3");                for (int i = 0 ;imap = new HashMap<>();                    String zhengtilujin = fileList.get(i).toString();                    String wenjianming = zhengtilujin.substring(zhengtilujin.lastIndexOf("/")+1);                    map.put("zhengtilujin",zhengtilujin);                    map.put("wenjianming",wenjianming);                    list.add(map);                }                Message message = new Message();                message.what = 1;                handler.sendMessage(message);            }        }).start();        Student student = new Student.Builder()                .setAddress()                .setAge()                .setName(                .builder();    }    /**    * 读取sd卡上指定后缀的所有文件    * @param files 返回的所有文件    * @param filePath 路径(可传入sd卡路径)    * @param suffere 后缀名称 比如 .gif    * @return    */    public ListgetSuffixFile(Listfiles, String filePath, String suffere) {

    File f = new File(filePath);

    if (!f.exists()) {

    return null;

    }

    File[] subFiles = f.listFiles();

    for (File subFile : subFiles) {

    if(subFile.isFile() && subFile.getName().endsWith(suffere)){

    files.add(subFile);

    } else if(subFile.isDirectory()){

    getSuffixFile(files, subFile.getAbsolutePath(), suffere);

    } else{

    //非指定目录文件 不做处理

    }

    }

    return files;

    }

    @Override

    public boolean handleMessage(Message message) {

    switch (message.what){

    case 1:

    adapters.notifyDataSetChanged();

    imageView.setVisibility(View.GONE);

    listView.setVisibility(View.VISIBLE);

    break;

    }

    return true;

    }

    }

    Adapter

    public class Adapters extends BaseAdapter{    private Context context;    private List> list;

    public Adapters(Context context,List list){

    this.context = context;

    this.list= list;

    }

    @Override

    public int getCount() {

    return list.size();

    }

    @Override

    public Object getItem(int i) {

    return list.get(i);

    }

    @Override

    public long getItemId(int i) {

    return i;

    }

    @Override

    public View getView(int i, View view, ViewGroup viewGroup) {

    ViewHolder viewHolder  = null;

    if (view==null){

    viewHolder = new ViewHolder();

    view = LayoutInflater.from(context).inflate(R.layout.item_layout,viewGroup,false);

    viewHolder.imageView = (ImageView) view.findViewById(R.id.imageView);  //  改变当前adapter  imageView样式

    viewHolder.textView  = (TextView) view.findViewById(R.id.textView);

    view.setTag(viewHolder);

    }else {

    viewHolder = (ViewHolder) view.getTag();

    }

    //        map.put("zhengtilujin",zhengtilujin);

    //        map.put("wenjianming",wenjianming);

    viewHolder.textView.setText(list.get(i).get("wenjianming").toString());

    return view;

    }

    class ViewHolder {

    ImageView imageView ;

    TextView textView;

    }

    }

    二、后来又需求要遍历文件中多种格式文件,我又改了改,可能逻辑有点繁琐但也能解决问题,等以后再优化吧,希望可以给大家个思路

    private List<Map<String,Object>>list= new ArrayList<>();

    File path = Environment.getExternalStorageDirectory();// 获得SD卡路径

    final File [] files = path.listFiles();

    if (files != null) {// 先判断目录是否为空,否则会报空指针

    for (File file : files) {

    if (file.isDirectory()) {

    getFileName(file.listFiles());

    } else {

    String fileName = file.getName();

    HashMap map = new HashMap();

    if (fileName.endsWith(".txt")) {

    map.put("Name", fileName.substring(0, fileName.lastIndexOf(".")) + ".txt");

    list.add(map);

    } else if (fileName.endsWith(".pdf")) {

    map.put("Name", fileName.substring(0, fileName.lastIndexOf(".")) + ".pdf");

    list.add(map);

    } else if (fileName.endsWith(".docx")) {

    map.put("Name", fileName.substring(0, fileName.lastIndexOf(".")) + ".docx");

    list.add(map);

    } else if (fileName.endsWith(".xlsx")) {

    map.put("Name", fileName.substring(0, fileName.lastIndexOf(".")) + ".xlsx");

    list.add(map);

    } else if (fileName.endsWith(".pptx")) {

    map.put("Name", fileName.substring(0, fileName.lastIndexOf(".")) + ".pptx");

    list.add(map);

    }

    }

    }

    }

    相关文章

      网友评论

          本文标题:Android 读取SD卡指定后缀的文件

          本文链接:https://www.haomeiwen.com/subject/xpakmttx.html