Androin学习笔记四十八:弹出框选择文件夹目录 以及启用新的Task打开文件

出处:http://blog.csdn.net/yf210yf/article/details/7863196

首先看效果图

 

第一个Activity很简单就是一个按钮Button 加一个 TextView

见main.xml

[c-sharp] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout   
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent"  
  6.   android:orientation="vertical"  
  7.   android:background="@drawable/white"  
  8. >  
  9. <Button       
  10.     android:id="@+id/button"    
  11.     android:layout_width="fill_parent"      
  12.     android:layout_height="wrap_content"      
  13.     android:text="选择文件"    
  14.     />  
  15. <TextView  
  16.     android:id="@+id/fileText"  
  17.     android:gravity="center"  
  18.     android:textSize="20px"  
  19.     android:textColor="#219ac6"  
  20.     android:layout_width="fill_parent"      
  21.     android:layout_height="fill_parent"      
  22.     />  
  23. </LinearLayout>  

Button弹出显示文件夹选择框

TextView就是用来显示当前选择的文件夹路径

[c-sharp] view plaincopy
  1. public class MainAcivity extends Activity{  
  2.       
  3.     public static final int FILE_RESULT_CODE = 1;  
  4.       
  5.     private TextView textView;  
  6.       
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.main);  
  11.         Button button = (Button)findViewById(R.id.button);  
  12.         textView = (TextView)findViewById(R.id.fileText);  
  13.         button.setOnClickListener(new OnClickListener() {  
  14.               
  15.             public void onClick(View v) {  
  16.                 Intent intent = new Intent(MainAcivity.this,MyFileManager.class);  
  17.                 startActivityForResult(intent, FILE_RESULT_CODE);  
  18.             }  
  19.         });  
  20.     }  
  21.       
  22.     @Override  
  23.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  24.         if(FILE_RESULT_CODE == requestCode){  
  25.             Bundle bundle = null;  
  26.             if(data!=null&&(bundle=data.getExtras())!=null){  
  27.                 textView.setText("选择文件夹为:"+bundle.getString("file"));  
  28.             }  
  29.         }  
  30.     }  
  31. }  

这里通过startActivityForResult()弹出的Activity  所以要重写onActivityResult方法

在目标Activity调用方法finish()的时候会回调

这里需要注意在按返回键时也会调用该方法 所以这里还需要判空

然后再 MyFileManager 这个Activity中通过ListView来显示文件列表

布局文件 fileselect.xml

[c-sharp] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout   
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="250px"  
  5.   android:layout_height="400px"  
  6.   android:orientation="vertical"  
  7.   android:background="@drawable/white"  
  8. >  
  9.   <TextView   
  10.     android:id="@+id/mPath"  
  11.     android:layout_width="wrap_content"  
  12.     android:layout_height="wrap_content"  
  13.     android:padding="5px"  
  14.     android:textSize="18sp"  
  15.     android:textColor="@drawable/blue"  
  16.   >  
  17.   </TextView>  
  18.   <ListView   
  19.     android:id="@android:id/list"  
  20.     android:layout_width="fill_parent"  
  21.     android:layout_height="330px"  
  22.   >  
  23.   </ListView>  
  24.   <LinearLayout   
  25.     android:gravity="center"  
  26.     android:layout_width="wrap_content"  
  27.     android:layout_height="wrap_content"  
  28.     android:orientation="horizontal"  
  29.     android:background="@drawable/white"  
  30.     >  
  31.     <Button       
  32.     android:id="@+id/buttonConfirm"    
  33.     android:layout_width="125px"      
  34.     android:layout_height="fill_parent"      
  35.     android:text="确定"    
  36.     />  
  37.     <Button       
  38.     android:id="@+id/buttonCancle"    
  39.     android:layout_width="125px"      
  40.     android:layout_height="fill_parent"      
  41.     android:text="取消"    
  42.     />  
  43.       
  44.   </LinearLayout>  
  45. </LinearLayout>  

列表显示Item 通过布局文件 file_row.xml

[c-sharp] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:orientation="horizontal"  
  5.   android:layout_width="fill_parent"  
  6.   android:layout_height="fill_parent"  
  7.   android:background="#ffffff"      
  8. >  
  9. <LinearLayout    
  10.         android:orientation="horizontal"    
  11.         android:layout_width="fill_parent"    
  12.         android:layout_height="fill_parent"    
  13.         android:background="@drawable/listview_selected"    
  14.         android:padding="6px"    
  15.     >    
  16.   <ImageView android:id="@+id/icon"  
  17.     android:layout_width="30dip"  
  18.     android:layout_height="30dip"  
  19.   >  
  20.   </ImageView>  
  21.   <TextView android:id="@+id/text"  
  22.     android:layout_gravity="center_horizontal"  
  23.     android:layout_width="fill_parent"  
  24.     android:layout_height="wrap_content"   
  25.     android:textColor="@drawable/black"  
  26.   >  
  27.   </TextView>  
  28.   </LinearLayout>  
  29. </LinearLayout>  

这里简单实现点击变换底色的效果 通过两层LinearLayout 的方式实现 创建一个select可以自定义点击效果 就不赘述了

文件选择代码

[c-sharp] view plaincopy
  1. package com.debby;  
  2. import java.io.File;  
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import android.app.ListActivity;  
  6. import android.content.Intent;  
  7. import android.net.Uri;  
  8. import android.os.Bundle;  
  9. import android.util.Log;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.view.Window;  
  13. import android.widget.Button;  
  14. import android.widget.ListView;  
  15. import android.widget.TextView;  
  16. public class MyFileManager extends ListActivity {  
  17.     private List<String> items = null;  
  18.     private List<String> paths = null;  
  19.     private String rootPath = "/";  
  20.     private String curPath = "/";  
  21.     private TextView mPath;  
  22.     private final static String TAG = "bb";  
  23.     @Override  
  24.     protected void onCreate(Bundle icicle) {  
  25.         super.onCreate(icicle);  
  26.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  27.         setContentView(R.layout.fileselect);  
  28.         mPath = (TextView) findViewById(R.id.mPath);  
  29.         Button buttonConfirm = (Button) findViewById(R.id.buttonConfirm);  
  30.         buttonConfirm.setOnClickListener(new OnClickListener() {  
  31.             public void onClick(View v) {  
  32.                 Intent data = new Intent(MyFileManager.this, MainAcivity.class);  
  33.                 Bundle bundle = new Bundle();  
  34.                 bundle.putString("file", curPath);  
  35.                 data.putExtras(bundle);  
  36.                 setResult(2, data);  
  37.                 finish();  
  38.             }  
  39.         });  
  40.         Button buttonCancle = (Button) findViewById(R.id.buttonCancle);  
  41.         buttonCancle.setOnClickListener(new OnClickListener() {  
  42.             public void onClick(View v) {  
  43.                 finish();  
  44.             }  
  45.         });  
  46.         getFileDir(rootPath);  
  47.     }  
  48.     private void getFileDir(String filePath) {  
  49.         mPath.setText(filePath);  
  50.         items = new ArrayList<String>();  
  51.         paths = new ArrayList<String>();  
  52.         File f = new File(filePath);  
  53.         File[] files = f.listFiles();  
  54.         if (!filePath.equals(rootPath)) {  
  55.             items.add("b1");  
  56.             paths.add(rootPath);  
  57.             items.add("b2");  
  58.             paths.add(f.getParent());  
  59.         }  
  60.         for (int i = 0; i < files.length; i++) {  
  61.             File file = files[i];  
  62.             items.add(file.getName());  
  63.             paths.add(file.getPath());  
  64.         }  
  65.         setListAdapter(new MyAdapter(this, items, paths));  
  66.     }  
  67.     @Override  
  68.     protected void onListItemClick(ListView l, View v, int position, long id) {  
  69.         File file = new File(paths.get(position));  
  70.         if (file.isDirectory()) {  
  71.             curPath = paths.get(position);  
  72.             getFileDir(paths.get(position));  
  73.         } else {  
  74.             //可以打开文件  
  75.         }  
  76.     }  
  77. }  

 

[c-sharp] view plaincopy
  1. import java.io.File;  
  2. import java.util.List;  
  3. import com.debby.R;  
  4. import android.content.Context;  
  5. import android.graphics.Bitmap;  
  6. import android.graphics.BitmapFactory;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.widget.BaseAdapter;  
  11. import android.widget.ImageView;  
  12. import android.widget.TextView;  
  13. public class MyAdapter extends BaseAdapter  
  14. {  
  15.   private LayoutInflater mInflater;  
  16.   private Bitmap mIcon1;  
  17.   private Bitmap mIcon2;  
  18.   private Bitmap mIcon3;  
  19.   private Bitmap mIcon4;  
  20.   private List<String> items;  
  21.   private List<String> paths;  
  22.   public MyAdapter(Context context,List<String> it,List<String> pa)  
  23.   {  
  24.     mInflater = LayoutInflater.from(context);  
  25.     items = it;  
  26.     paths = pa;  
  27.     mIcon1 = BitmapFactory.decodeResource(context.getResources(),R.drawable.back01);  
  28.     mIcon2 = BitmapFactory.decodeResource(context.getResources(),R.drawable.back02);  
  29.     mIcon3 = BitmapFactory.decodeResource(context.getResources(),R.drawable.folder);  
  30.     mIcon4 = BitmapFactory.decodeResource(context.getResources(),R.drawable.doc);  
  31.   }  
  32.     
  33.   @Override  
  34.   public int getCount()  
  35.   {  
  36.     return items.size();  
  37.   }  
  38.   @Override  
  39.   public Object getItem(int position)  
  40.   {  
  41.     return items.get(position);  
  42.   }  
  43.     
  44.   @Override  
  45.   public long getItemId(int position)  
  46.   {  
  47.     return position;  
  48.   }  
  49.     
  50.   @Override  
  51.   public View getView(int position,View convertView,ViewGroup parent)  
  52.   {  
  53.     ViewHolder holder;  
  54.       
  55.     if(convertView == null)  
  56.     {  
  57.       convertView = mInflater.inflate(R.layout.file_row, null);  
  58.       holder = new ViewHolder();  
  59.       holder.text = (TextView) convertView.findViewById(R.id.text);  
  60.       holder.icon = (ImageView) convertView.findViewById(R.id.icon);  
  61.         
  62.       convertView.setTag(holder);  
  63.     }  
  64.     else  
  65.     {  
  66.       holder = (ViewHolder) convertView.getTag();  
  67.     }  
  68.     File f=new File(paths.get(position).toString());  
  69.     if(items.get(position).toString().equals("b1"))  
  70.     {  
  71.       holder.text.setText("返回根目录..");  
  72.       holder.icon.setImageBitmap(mIcon1);  
  73.     }  
  74.     else if(items.get(position).toString().equals("b2"))  
  75.     {  
  76.       holder.text.setText("返回上一层..");  
  77.       holder.icon.setImageBitmap(mIcon2);  
  78.     }  
  79.     else  
  80.     {  
  81.       holder.text.setText(f.getName());  
  82.       if(f.isDirectory())  
  83.       {  
  84.         holder.icon.setImageBitmap(mIcon3);  
  85.       }  
  86.       else  
  87.       {  
  88.         holder.icon.setImageBitmap(mIcon4);  
  89.       }  
  90.     }  
  91.     return convertView;  
  92.   }  
  93.   private class ViewHolder  
  94.   {  
  95.     TextView text;  
  96.     ImageView icon;  
  97.   }  
  98. }  

在 onListItemClick 事件中改变ListView的Adapter实现打开文件夹

这里点击文件夹时可以打开目录,如果选择文件可以根据类型来启用不同工具来打开

方法如下

[c-sharp] view plaincopy
  1. private void openFile(File f) {  
  2.         Intent intent = new Intent();  
  3.         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  4.         intent.setAction(android.content.Intent.ACTION_VIEW);  
  5.         String type = getMIMEType(f);  
  6.         intent.setDataAndType(Uri.fromFile(f), type);  
  7.         startActivity(intent);  
  8.     }  
  9.     private String getMIMEType(File f) {  
  10.         String type = "";  
  11.         String fName = f.getName();  
  12.         String end = fName  
  13.                 .substring(fName.lastIndexOf(".") + 1, fName.length())  
  14.                 .toLowerCase();  
  15.         if (end.equals("m4a") || end.equals("mp3") || end.equals("mid")  
  16.                 || end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {  
  17.             type = "audio";  
  18.         } else if (end.equals("3gp") || end.equals("mp4")) {  
  19.             type = "video";  
  20.         } else if (end.equals("jpg") || end.equals("gif") || end.equals("png")  
  21.                 || end.equals("jpeg") || end.equals("bmp")) {  
  22.             type = "image";  
  23.         } else {  
  24.             type = "*";  
  25.         }  
  26.         type += "/*";  
  27.         return type;  
  28.     }  

根据文件的后缀名来确定type 然后打开文件,打开方式通过setDataAndType方法设置


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值