Android开发之简单文件管理器实现方法

2019-12-10 19:29:13丽君

自定义适配器:

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://www.easck.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>