Home  >  Article  >  Java  >  Simple implementation of Android writing file browser

Simple implementation of Android writing file browser

高洛峰
高洛峰Original
2017-01-17 15:04:281435browse

Sometimes we always use the file browser function to save files. So today the blogger will show you how to make one.

So to start, to browse files, we need to write a file tool class.

import java.io.File; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
  
import android.util.Log; 
  
public class FileUtils { 
  /** 
   * 获取当前目录下的所有文件或文件夹 
   * @param path 路径 
   * @return 
   */
  public static List<Map<String,Object>> GetPathFilsList(String path) { 
    List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); 
    List<Map<String,Object>> filelist = new ArrayList<Map<String,Object>>(); 
    try { 
      String[] Files = new File(path).list(); 
      for(String file : Files){ 
        Map<String, Object> map = new HashMap<String, Object>(); 
        if(new File(path+file).isDirectory()){ 
          map.put("isDirectory",2); 
          map.put("fileName", file); 
            
          list.add(map); 
        }else { 
          map.put("isDirectory", 1); 
          map.put("fileName", file); 
            
          filelist.add(map); 
        } 
        
          
      } 
      list.addAll(filelist); 
      return list; 
    } catch (Exception e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
      return null; 
    } 
  } 
}

The files and folders will be distinguished here to facilitate display and distinction.

After that we need to use an adapter, a ListView used to display these data

private class FileBrowserAdapter extends BaseAdapter{ 
    private List<Map<String, Object>> fileList; 
    private Context context; 
  
    public FileBrowserAdapter(Context Context, 
        List<Map<String, Object>> fileList) { 
      this.fileList = fileList; 
      this.context = context; 
    } 
  
    @Override
    public int getCount() { 
      return fileList == null ? 0 : fileList.size(); 
    } 
  
    @Override
    public Object getItem(int position) { 
      return fileList.get(position); 
    } 
  
    @Override
    public long getItemId(int position) { 
      return position; 
    } 
  
    @SuppressLint("InflateParams") 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) { 
  
      LayoutInflater mInflater = LayoutInflater 
          .from(getApplicationContext()); 
      View view = null; 
      view = mInflater.inflate(R.layout.file_list_item, null); 
      ImageView image = (ImageView) view 
          .findViewById(R.id.file_list_item_image); 
      if (Integer 
          .parseInt(fileList.get(position).get("isDirectory") + "") == 2) 
        image.setImageResource(R.drawable.folder); 
      else if (Integer.parseInt(fileList.get(position).get("isDirectory") 
          + "") == 1) 
        image.setImageResource(R.drawable.documents); 
      TextView textView = (TextView) view 
          .findViewById(R.id.file_list_item_testview); 
      textView.setTextColor(Color.BLACK); 
      textView.setText(fileList.get(position).get("fileName") + ""); 
      return view; 
  
    } 
    
  }

Finally we need to implement these

dir = Environment.getExternalStorageDirectory() 
        .getAbsolutePath() + "/"; 
    fileListView = (ListView) findViewById(R.id.file_listview); 
    listItemClickListener = new FileListItemClickListener(); 
//   //设置点击事件 
    fileListView.setOnItemClickListener(listItemClickListener); 
    fileList = FileUtils.GetPathFilsList(dir); 
    if (new File(dir).getParent() != null) { 
      Map<String, Object> map = new HashMap<String, Object>(); 
      map.put("isDirectory", 0); 
      map.put("fileName", new File(dir).getParent()); 
      fileList.add(0, map); 
  
    } 
    FileBrowserAdapter phoneFileBrowserAdapter = new FileBrowserAdapter( 
        getApplicationContext(), fileList); 
    fileListView.setAdapter(phoneFileBrowserAdapter);

Renderings:

Simple implementation of Android writing file browser

The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.

For more articles related to the simple implementation of Android writing file browser, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn