大概流程:
点击选择文件按钮,出来MyFileManager对话框,从而选择文件路径,获取文件路径,对文件进行读取,并插入到数据库中
选取文件获取文件名及路径方法 PS:被屏蔽掉的代码,如果恢复正常,则可以判断文件类型,从而进行打开
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class MyFileManager extends ListActivity {
private List<String> items = null;
private List<String> paths = null;
private String rootPath = "/";
private String curPath = "/";
private TextView mPath;
private final static String TAG = "bb";
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.fileselect);
mPath = (TextView) findViewById(R.id.mPath);
// Button buttonConfirm = (Button) findViewById(R.id.buttonConfirm);
// buttonConfirm.setOnClickListener(new OnClickListener() {
//
// public void onClick(View v) {
// Intent data = new Intent(MyFileManager.this, MainActivity.class);
// Bundle bundle = new Bundle();
// bundle.putString("file", curPath);
// data.putExtras(bundle);
// setResult(2, data);
// finish();
//
// }
// });
// Button buttonCancle = (Button) findViewById(R.id.buttonCancle);
// buttonCancle.setOnClickListener(new OnClickListener() {
//
// public void onClick(View v) {
// finish();
// }
// });
getFileDir(rootPath);
}
private void getFileDir(String filePath) {
mPath.setText(filePath);
items = new ArrayList<String>();
paths = new ArrayList<String>();
File f = new File(filePath);
File[] files = f.listFiles();
if (!filePath.equals(rootPath)) {
items.add("b1");
paths.add(rootPath);
items.add("b2");
paths.add(f.getParent());
}
for (int i = 0; i < files.length; i++) {
File file = files[i];
items.add(file.getName());
paths.add(file.getPath());
}
setListAdapter(new MyAdapter(this, items, paths));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
File file = new File(paths.get(position));
if (file.isDirectory()) {
curPath = paths.get(position);
getFileDir(paths.get(position));
}else{
// String fName = file.getName();
curPath=paths.get(position);
Intent data = new Intent(MyFileManager.this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("file", curPath);
data.putExtras(bundle);
setResult(2, data);
finish();
}
// else {
// openFile(file);
// }
}
// private void openFile(File f) {
// Intent intent = new Intent();
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// intent.setAction(android.content.Intent.ACTION_VIEW);
//
// String type = getMIMEType(f);
// intent.setDataAndType(Uri.fromFile(f), type);
// startActivity(intent);
// }
// private String getMIMEType(File f) {
// String type = "";
// String fName = f.getName();
// String end = fName
// .substring(fName.lastIndexOf(".") + 1, fName.length())
// .toLowerCase();
//
// if (end.equals("m4a") || end.equals("mp3") || end.equals("mid")
// || end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {
// type = "audio";
// } else if (end.equals("3gp") || end.equals("mp4")) {
// type = "video";
// } else if (end.equals("jpg") || end.equals("gif") || end.equals("png")
// || end.equals("jpeg") || end.equals("bmp")) {
// type = "image";
// } else {
// type = "*";
// }
// type += "/*";
// return type;
// }
}
读取文件并向手机数据库中插入文件记录适配器
public class MyDBAdapter {
private static String fileName;
private static final String DATABASE_NAME="sense.db";
private static final String DATABASE_TABLE="sense";
private static final int DATABASE_VERSION=1;
private static final String DATABASE_CREATE="create table "+DATABASE_TABLE+"(_id integer primary key autoincrement, lac char(8),cid char(8),name varchar(40))";
private SQLiteDatabase db;
private final Context context;
private myDbHelper dbHelper;
public MyDBAdapter(Context context,String fileName){
this.context=context;
this.fileName=fileName;
dbHelper=new myDbHelper(context,DATABASE_NAME,null,DATABASE_VERSION);
}
public MyDBAdapter(Context context){
this.context=context;
dbHelper=new myDbHelper(context,DATABASE_NAME,null,DATABASE_VERSION);
}
// public boolean existDatabase(){
// boolean flag=false;
// try{
//
// flag=true;
// }catch(FileNotFoundException e){
// flag=false;
// }
// return flag;
// }
public boolean getcount(){
boolean flag=false;
Cursor cursor = null;
try {
cursor= db.query(DATABASE_TABLE,null,null,null,null,null,null);
System.out.println("总数据"+cursor.getCount());