android 查找某个特定文件后缀名

 private void queryFiles(){
        String[] projection = new String[] { MediaStore.Files.FileColumns._ID,
            MediaStore.Files.FileColumns.DATA,
            MediaStore.Files.FileColumns.SIZE
        };
        Cursor cursor = getContentResolver().query(
            Uri.parse("content://media/external/file"),
            projection,
            MediaStore.Files.FileColumns.DATA + " like ?",
            new String[]{"%.txt"},
            null);

        if (cursor != null) {
            if (cursor.moveToFirst()) {

                int idindex = cursor
                    .getColumnIndex(MediaStore.Files.FileColumns._ID);
                int dataindex = cursor
                    .getColumnIndex(MediaStore.Files.FileColumns.DATA);
                int sizeindex = cursor
                    .getColumnIndex(MediaStore.Files.FileColumns.SIZE);
                do {
                    String id = cursor.getString(idindex);
                    String path = cursor.getString(dataindex);
                    String size = cursor.getString(sizeindex);
                    docBean.setId(id);
                    docBean.setPath(path);
                    docBean.setSize(size);
                    int dot=path.lastIndexOf("/");
                    String name=path.substring(dot+1);
                    Log.e("test",name);
                } while (cursor.moveToNext());
            }
        }
        cursor.close();
    }

 

posted @ 2016-09-01 20:40  麦子TMAC  阅读(2570)  评论(0编辑  收藏  举报