读取android目录指定文件,通过该类可读取指定sdCard目录中的文件,对文件进行重命名,查看属性,显示文件大小,删除,打开操作,主要代码我已经加了注释,不妨各位自己看看,我就不多说了,代码如下:
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.cykj.oa.app.R;
import com.cykj.oa.app.adapter.FileListAdapter;
import com.cykj.oa.app.bean.FileBean;
import com.cykj.oa.app.utils.FileUtil;
public class DocumentManage extends ListActivity implements OnItemLongClickListener, OnItemSelectedListener, OnItemClickListener {
String SDPATH = Environment.getExternalStorageDirectory() + "/OA-APP/";
private static final String[] operate = { "删除", "重命名", "属性" };
private View newAlertView;
private EditText newFileName_EditText;
private static String currentDirctory = "/mnt/sdcard/APP";
ScanFileTask scanFileTask;//扫描文件的任务
private List<FileBean> files = new ArrayList<FileBean>();//FileBean有两个属性filename,filerul
private boolean isListViewShow = true;
private FileListAdapter fileListAdapter;
private int currentItem = 0;
private boolean out=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.listview_documentmanage);
if (checkSDCard()) {// 加载文件列表
fileListAdapter = new FileListAdapter(this, files, FileUtil.isZoom);
setListAdapter(fileListAdapter);
getListView().setOnItemLongClickListener(this);//长按显示上下文菜单
getListView().setOnItemSelectedListener(this);//指的是单击一个条目
getListView().setOnItemClickListener(this);
scanFile(FileUtil.rootPath);//文件扫描
}
}
/* 检查SDCard是否挂载 */
public boolean checkSDCard() {
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
showMessage(getStr(com.cykj.oa.app.R.string.no_sdcard));
return false;
}
return true;
}
// 短消息提示
public void showMessage(String text) {
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
}
// 从资源获取字符串
public String getStr(int ID) {
return getResources().getString(ID);
}
/* 文件打开处理 */
public void openFile(File f) {
Intent intent = new Intent();
// 采用系统默认打开方式
//Log.i(TAG, "type123="+FileUtil.isOpen );
//FileUtil.isOpen=1;
if (FileUtil.isOpen == 0) {
String type = FileUtil.getMIMEType(f);
// 打开图片浏览器
if (type.equals("image")) {
// intent.setClass(MainActivity.this, BrowseImagesActivity.class);
intent.putStringArrayListExtra("images", FileUtil.searchImages(f));
startActivity(intent);
}
// 打开媒体播放器
else if (type.equals("audio") || type.equals("video")) {
// intent.setClass(MainActivity.this, MediaPlayerActivity.class);
Bundle bl = new Bundle();
bl.putString("mediapath", f.getAbsolutePath());
bl.putString("mediatype", type);
intent.putExtras(bl);
startActivity(intent);
}
else if(type.equals("txt")){
intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent