在我們的手持設備中,一般都會自帶設備公司自己開發的文件管理系統、拍照系統之類的東東,今天我給大伙說說入門級開發的文件夾管理器,代碼贼少
總共就6個類吧,沒有夾雜其他外部應用,就是一個純文件夾管理器
APP主要功能設計:文件復制、文件夾復制、批量文件/文件夾復制、刪除文件、刪除文件夾、批量刪除文件/文件夾、文件分類、文件搜索暫時沒有寫,
有興趣的可以自己寫寫
APP主要應用:DrawerLayout 、ListView、Fragment、IO、序列化、文件過濾、文件排序、ActionBar、Adapter、v4包、一些事件等
下面開始講開發吧,首先我們要給APP讀寫權限,在清單文件配置如下權限:
主界面活動代碼:
package com.example.maozhuafile;
/**
* 文件管理主界面
* @author Engineer-Jsp
* @date 2014.10.27
* */
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.maozhuafile.MainActivity.FragmentA.FragmentB;
import com.example.maozhuafile.MainActivity.FragmentA.FragmentC;
import com.example.maozhuafile.MainActivity.FragmentA.FragmentD;
import com.example.util.CustomFileFilter;
import com.example.util.FileOpreationUitl;
import com.example.util.FileSort;
import com.example.util.FragmentAdapter;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.ActionMode;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnItemClickListener {
private ActionBar actionbar;
private ActionBarDrawerToggle drawertoggle;
private DrawerLayout drawerLayout;
private ListView drawerlistview;
private String[] drawerdata;
private SimpleAdapter adapter;
private List
主布局:
<frameLayout
android:id="@+id/drawer_frame_a"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<frameLayout
android:id="@+id/drawer_frame_b"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
文件列表 Item 布局文件:
自定義Adapter,控制文件與應用圖片配對,隊號入座,ViewHolder 節約內存資源:
package com.example.util;
/**
* 自定義容器Adapter
* @author Engineer-Jsp
* @date 2014.10.27
* */
import java.io.File;
import java.util.Date;
import com.example.maozhuafile.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class FragmentAdapter extends BaseAdapter {
private Context mContext ;
private File[] mfileData ;
private LayoutInflater mlayoutinflater;
public FragmentAdapter( File[]files ,Context pContext) {
mContext = pContext ;
mfileData = files ;
mlayoutinflater = LayoutInflater.from(mContext);
}
public void updata(File[] pfile){
mfileData = pfile ;
}
public int getCount() {
return mfileData ==null? 0: mfileData.length;
}
public Object getItem(int position) {
return mfileData[position];
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public ImageView mFileImage ;
public TextView mFileName ;
public TextView mFileTime;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder ;
View _view = convertView ;
if(_view ==null){
mViewHolder = new ViewHolder() ;
_view = mlayoutinflater.inflate(R.layout.file_listview, null) ;
mViewHolder.mFileImage =(ImageView) _view.findViewById(R.id.filelist_imageview
) ;
mViewHolder.mFileName = (TextView) _view.findViewById(R.id.filelist_textview) ;
mViewHolder.mFileTime = (TextView) _view.findViewById(R.id.filelist_timeText);
_view.setTag(mViewHolder) ;
}else{
mViewHolder = (ViewHolder) _view.getTag() ;
}
// 目錄的顯示特點
if(mfileData[position].isDirectory() && mfileData[position].canRead()){
// 文件夾分為空與非空
if(mfileData[position].listFiles().length == 0 && mfileData[position].listFiles()== null ){
mViewHolder.mFileImage.setImageResource(R.drawable.wenjian) ;
mViewHolder.mFileName.setText(mfileData[position].getName()) ;
mViewHolder.mFileTime.setText(new Date(System.currentTimeMillis()).toLocaleString());
}else{
mViewHolder.mFileImage.setImageResource(R.drawable.wenjianjia) ;
mViewHolder.mFileName.setText(mfileData[position].getName()) ;
mViewHolder.mFileTime.setText(new Date(System.currentTimeMillis()).toLocaleString());
}
}else{ //文件的處理
String _FileName = mfileData[position].getName().toLowerCase() ;
if(_FileName.endsWith(".txt")){ //文本顯示t
mViewHolder.mFileImage.setImageResource(R.drawable.wenjian) ;
mViewHolder.mFileName.setText(_FileName) ;
mViewHolder.mFileTime.setText(new Date(System.currentTimeMillis()).toLocaleString());
}else if(_FileName.endsWith(".png") || _FileName.endsWith(".jpg") ||_FileName.endsWith(".jpeg") || _FileName.endsWith(".gif")){
mViewHolder.mFileImage.setTag(mfileData[position].getAbsolutePath()) ;
mViewHolder.mFileImage.setImageResource(R.drawable.ic_action_picture);
mViewHolder.mFileName.setText(_FileName) ;
mViewHolder.mFileTime.setText(new Date(System.currentTimeMillis()).toLocaleString());
}else if(_FileName.endsWith(".mp4")|| _FileName.endsWith(".avi")|| _FileName.endsWith(".3gp") || _FileName.endsWith(".rmvb")){
mViewHolder.mFileImage.setImageResource(R.drawable.ic_action_video) ;
mViewHolder.mFileName.setText(_FileName) ;
mViewHolder.mFileTime.setText(new Date(System.currentTimeMillis()).toLocaleString());
}else if(_FileName.endsWith("mp3")){
mViewHolder.mFileImage.setImageResource(R.drawable.mp3) ;
mViewHolder.mFileName.setText(mfileData[position].getName()) ;
mViewHolder.mFileTime.setText(new Date(System.currentTimeMillis()).toLocaleString());
}else if(_FileName.endsWith("doc")){
mViewHolder.mFileImage.setImageResource(R.drawable.word) ;
mViewHolder.mFileName.setText(mfileData[position].getName()) ;
mViewHolder.mFileTime.setText(new Date(System.currentTimeMillis()).toLocaleString());
}
}
/* View view = convertView ;
view =mlayoutinflater.inflate(com.example.jsp.R.layout.list_item, null) ;
TextView tv = (TextView)view.findViewById(com.example.jsp.R.id.tv_name1) ;
tv.setText(mfileData[position].getName()+"") ;*/
return _view;
}
// public static void main(String[] args) {
// System.out.println(new Date(System.currentTimeMillis()).toString());
// }
}
至此主活動布局就完成了,主布局效果圖:
注意:如果使用模擬器測試,請注意,如果模擬器沒有開啟 SD 內存iso,請自行開啟測試,如果SD內存為空,請自行新建文件測試,謝謝~