技术与热点应用 您所在的位置:电子展览网 > 行业资讯 > 技术与热点应用 > 手机设计与制造

Android 快速实现一个文件选择器




关键词:

  最近在应用当中需要能够就/sdcard 上面的文件进行选择后,上传.

  就需要一个文件选择器来使用.

  找了好些别人的代码,准备使用.发现实现的有些麻烦.或者说引用起来有些麻烦.就自己实现了一个.

  本着简单易用的原则,就一个java搞定.(后来发现还是需要2个java 得有一个定义个公用的结构类.)

  具体要求 /sdcard 是根目录.

  只显示文件夹和指定后缀名的文件.根据指定的文件后缀,显示不同的文件图标.

  截图

  实现时需要使用listview 和BaseAdapter.

  实际上熟练掌握这个2个类,是非常有必要的,你的大部分Android app都在和他们打交道.

  首先是结构类.说起结构来就觉得java 最大的缺点不支持结构,好麻烦.

  public class fileData{

  public String name;

  public int type;

  }

  public class fileData{

  public String name;

  public int type;

  }

  然后就是实现文件选择器的BaseAdapter

  private class ExpBaseAdapter extends BaseAdapter {

  private Context mContext;

  private Vector mItems = new Vector();

  private LinearLayout layout,layout_more;

  public ExpBaseAdapter(Context context) {

  mContext = context;

  }

  public void addItem(fileData it) {

  mItems.add(it);

  }

  public fileData getItem(int it) {

  return (fileData)mItems.elementAt(it);

  }

  public int getCount() {

  // TODO Auto-generated method stub

  return mItems.size();

  }

  public long getItemId(int arg0) {

  // TODO Auto-generated method stub

  return arg0;

  }

  public int getItemType(int arg0) {

  // TODO Auto-generated method stub

  return getItem(arg0).type;

  }

  public void clearItems(){

  mItems.clear();

第1页 第2页 第3页 第4页 第5页 

  最近在应用当中需要能够就/sdcard 上面的文件进行选择后,上传.

  就需要一个文件选择器来使用.

  找了好些别人的代码,准备使用.发现实现的有些麻烦.或者说引用起来有些麻烦.就自己实现了一个.

  本着简单易用的原则,就一个java搞定.(后来发现还是需要2个java 得有一个定义个公用的结构类.)

  具体要求 /sdcard 是根目录.

  只显示文件夹和指定后缀名的文件.根据指定的文件后缀,显示不同的文件图标.

  截图

  实现时需要使用listview 和BaseAdapter.

  实际上熟练掌握这个2个类,是非常有必要的,你的大部分android app都在和他们打交道.

  首先是结构类.说起结构来就觉得java 最大的缺点不支持结构,好麻烦.

  public class fileData{

  public String name;

  public int type;

  }

  public class fileData{

  public String name;

  public int type;

  }

  然后就是实现文件选择器的BaseAdapter

  private class ExpBaseAdapter extends BaseAdapter {

  private Context mContext;

  private Vector mItems = new Vector();

  private LinearLayout layout,layout_more;

  public ExpBaseAdapter(Context context) {

  mContext = context;

  }

  public void addItem(fileData it) {

  mItems.add(it);

  }

  public fileData getItem(int it) {

  return (fileData)mItems.elementAt(it);

  }

  public int getCount() {

  // TODO Auto-generated method stub

  return mItems.size();

  }

  public long getItemId(int arg0) {

  // TODO Auto-generated method stub

  return arg0;

  }

  public int getItemType(int arg0) {

  // TODO Auto-generated method stub

  return getItem(arg0).type;

  }

  public void clearItems(){

  mItems.clear();

第1页 第2页 第3页 第4页 第5页 

  接上页

  }

  public View getView(int arg0, View arg1, ViewGroup arg2) {

  // TODO Auto-generated method stub

  //LinearLayout layout=null ;

  LayoutInflater inflate = (LayoutInflater) mContext.getSystemService(android.content.Context.LAYOUT_INFLATER_SERVICE);

  arg1 = (LinearLayout) inflate.inflate(R.layout.listfile,null);

  TextView fm_text_01= (TextView)arg1.findViewById(R.id.fm_text_01);

  fm_text_01.setText(getItem(arg0).name);

  //Log.v("vodone",""+arg0+" name="+getItem(arg0).name);

  /*if (getItem(arg0).equals(".")){

  }*/

  return arg1 ;

  }

  }

  private class ExpBaseAdapter extends BaseAdapter {

  private Context mContext;

  private Vector mItems = new Vector();

  private LinearLayout layout,layout_more;

  public ExpBaseAdapter(Context context) {

  mContext = context;

  }

  public void addItem(fileData it) {

  mItems.add(it);

  }

  public fileData getItem(int it) {

  return (fileData)mItems.elementAt(it);

  }

  public int getCount() {

  // TODO Auto-generated method stub

  return mItems.size();

  }

  public long getItemId(int arg0) {

  // TODO Auto-generated method stub

  return arg0;

  }

  public int getItemType(int arg0) {

  // TODO Auto-generated method stub

  return getItem(arg0).type;

  }

  public void clearItems(){

  mItems.clear();

  }

  public View getView(int arg0, View arg1, ViewGroup arg2) {

  // TODO Auto-generated method stub

  //LinearLayout layout=null ;

  LayoutInflater inflate = (LayoutInflater) mContext.getSystemService(android.content.Context.LAYOUT_INFLATER_SERVICE);

  arg1 = (LinearLayout) inflate.inflate(R.layout.listfile,null);

第1页 第2页 第3页 第4页 第5页 

  接上页

  TextView fm_text_01= (TextView)arg1.findViewById(R.id.fm_text_01);

  fm_text_01.setText(getItem(arg0).name);

  //Log.v("vodone",""+arg0+" name="+getItem(arg0).name);

  /*if (getItem(arg0).equals(".")){

  }*/

  return arg1 ;

  }

  }

  最后来实现listview

  public class frm_openfile extends Activity implements OnClickListener {

  ListView lv2;

  private File mcurrentDirectory = new File("/sdcard/");

  ExpBaseAdapter ap;

  String fileEndings[]={"png","bmp","jpg","jpeg","avi","3gp","mp3","mp4"};

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setTitle(R.string.text_selectfile);

  setContentView(R.layout.frm_openfile);

  lv2 = (ListView)findViewById(R.id.of_lv);

  lv2.setCacheColorHint(0x00000000);

  ap=new ExpBaseAdapter(this);

  lv2.setAdapter(ap);

  ListView.OnItemClickListener lv2click = new ListView.OnItemClickListener() {

  public void onItemClick(AdapterView parent, View view,

  int position, long id) {

  // "_id", "ext_number", "name","ann", "intro"

  int fid=ap.getItemType((int)id);

  String mPath="";

  if (fid==1){

  String s1=ap.getItem((int)id).name;

  if (s1.equals("..")){

  mPath=mCurrentDirectory.getParent();

  }else{

  mPath=mCurrentDirectory.getPath()+"/"+s1+"/";

  }

  mCurrentDirectory=new File(mPath);

  ListFile(mCurrentDirectory);

  }else{

  Bundle bundle = new Bundle();

  bundle.putString("filename",mCurrentDirectory.getPath()+"/"+ ap.getItem((int)id).name);

  Intent mIntent = new Intent();

  mIntent.putExtras(bundle);

  setResult(RESULT_OK, mIntent);

  frm_openfile.this.finish();

第1页 第2页 第3页 第4页 第5页 

  接上页

  }

  }

  };

  ListFile(mCurrentDirectory);

  lv2.setOnItemClickListener(lv2click);

  }

  private void ListFile(File aDirectory) {

  ap.clearItems();

  ap.notifyDataSetChanged();

  lv2.postInvalidate();

  Log.v("vodone","mpath="+aDirectory.getPath());

  //if (aDirectory==null)return;

  if (!aDirectory.getPath().equals("/sdcard")){

  fileData fd=new fileData();

  fd.name="..";

  fd.type=1;

  ap.addItem(fd);

  }

  for( File f : aDirectory.listFiles() ){

  if (f.isDirectory()){

  fileData fd=new fileData();

  ffd.name=f.getName();

  fd.type=1;

  ap.addItem(fd);

  }else{

  if (checkEnds(f.getName().toLowerCase())){

  fileData fd=new fileData();

  ffd.name=f.getName();

  fd.type=0;

  ap.addItem(fd);

  }

  }

  }

  ap.notifyDataSetChanged();

  lv2.postInvalidate();

  }

  private boolean checkEnds(String checkItsEnd) {

  for (String aEnd : fileEndings) {

  if (checkItsEnd.endsWith(aEnd))

  return true;

  }

  return false;

  }

  }

  public class frm_openfile extends Activity implements OnClickListener {

  ListView lv2;

  private File mCurrentDirectory = new File("/sdcard/");

  ExpBaseAdapter ap;

  String fileEndings[]={"png","bmp","jpg","jpeg","avi","3gp","mp3","mp4"};

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setTitle(R.string.text_selectfile);

  setContentView(R.layout.frm_openfile);

  lv2 = (ListView)findViewById(R.id.of_lv);

  lv2.setCacheColorHint(0x00000000);

  ap=new ExpBaseAdapter(this);

第1页 第2页 第3页 第4页 第5页 

  接上页

  lv2.setAdapter(ap);

  ListView.OnItemClickListener lv2click = new ListView.OnItemClickListener() {

  public void onItemClick(AdapterView parent, View view,

  int position, long id) {

  // "_id", "ext_number", "name","ann", "intro"

  int fid=ap.getItemType((int)id);

  String mPath="";

  if (fid==1){

  String s1=ap.getItem((int)id).name;

  if (s1.equals("..")){

  mPath=mCurrentDirectory.getParent();

  }else{

  mPath=mCurrentDirectory.getPath()+"/"+s1+"/";

  }

  mCurrentDirectory=new File(mPath);

  ListFile(mCurrentDirectory);

  }else{

  Bundle bundle = new Bundle();

  bundle.putString("filename",mCurrentDirectory.getPath()+"/"+ ap.getItem((int)id).name);

  Intent mIntent = new Intent();

  mIntent.putExtras(bundle);

  setResult(RESULT_OK, mIntent);

  frm_openfile.this.finish();

  }

  }

  };

  ListFile(mCurrentDirectory);

  lv2.setOnItemClickListener(lv2click);

  }

  private void ListFile(File aDirectory) {

  ap.clearItems();

  ap.notifyDataSetChanged();

  lv2.postInvalidate();

  Log.v("vodone","mpath="+aDirectory.getPath());

  //if (aDirectory==null)return;

  if (!aDirectory.getPath().equals("/sdcard")){

  fileData fd=new fileData();

  fd.name="..";

  fd.type=1;

  ap.addItem(fd);

  }

  for( File f : aDirectory.listFiles() ){

  if (f.isDirectory()){

  fileData fd=new fileData();

  fd.name=f.getName();

  fd.type=1;

  ap.addItem(fd);

  }else{

  if (checkEnds(f.getName().toLowerCase())){

  fileData fd=new fileData();

  fd.name=f.getName();

  fd.type=0;

  ap.addItem(fd);

  }

  }

  }

  ap.notifyDataSetChanged();

  lv2.postInvalidate();

  }

  private boolean checkEnds(String checkItsEnd) {

  for (String aEnd : fileEndings) {

  if (checkItsEnd.endsWith(aEnd))

  return true;

  }

  return false;

  }

  }

  很简单,是不?

第1页 第2页 第3页 第4页 第5页 

分享到:
收藏 打印
相关新闻
  • 没有相关信息!