自定义适配器:
- public class MyAdapter extends BaseAdapter{
- private LayoutInflater inflater;
- private Bitmap directory,file;
- //存储文件名称
- private ArrayList<String> names = null;
- //存储文件路径
- private ArrayList<String> paths = null;
- //参数初始化
- public MyAdapter(Context context,ArrayList<String> na,ArrayList<String> pa){
- names = na;
- paths = pa;
- directory = BitmapFactory.decodeResource(context.getResources(),R.drawable.d);
- file = BitmapFactory.decodeResource(context.getResources(),R.drawable.f);
- //缩小图片
- directory = small(directory,0.16f);
- file = small(file,0.1f);
- inflater = LayoutInflater.from(context);
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return names.size();
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return names.get(position);
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- ViewHolder holder;
- if (null == convertView){
- convertView = inflater.inflate(R.layout.file, null);
- holder = new ViewHolder();
- holder.text = (TextView)convertView.findViewById(R.id.textView);
- holder.image = (ImageView)convertView.findViewById(R.id.imageView);
- convertView.setTag(holder);
- }
- else {
- holder = (ViewHolder)convertView.getTag();
- }
- File f = new File(paths.get(position).toString());
- if (names.get(position).equals("@1")){
- holder.text.setText("/");
- holder.image.setImageBitmap(directory);
- }
- else if (names.get(position).equals("@2")){
- holder.text.setText("..");
- holder.image.setImageBitmap(directory);
- }
- else{
- holder.text.setText(f.getName());
- if (f.isDirectory()){
- holder.image.setImageBitmap(directory);
- }
- else if (f.isFile()){
- holder.image.setImageBitmap(file);
- }
- else{
- System.out.println(f.getName());
- }
- }
- return convertView;
- }
- private class ViewHolder{
- private TextView text;
- private ImageView image;
- }
- private Bitmap small(Bitmap map,float num){
- Matrix matrix = new Matrix();
- matrix.postScale(num, num);
- return Bitmap.createBitmap(map,0,0,map.getWidth(),map.getHeight(),matrix,true);
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:Android="http://schemas.android.com/apk/res/android"
- package="com.test.filemanager"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdk android:minSdkVersion="10" />
- <strong> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/></strong>
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".MainActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
运行结果如下:
查看目录文件

文件重命名:

删除文件:

打开文件:

更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11
