从android选取文件获取文件路径,并将文件读入到数据库中,有进度条显示

大概流程:

点击选择文件按钮,出来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());
            
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值