最近做毕设,一直在研究数据库方面的问题,学会了利用Assets,这个文件夹使用AssetManager类来进行访问。


Android要使用外部资源文件一般有两种方法,分别是:

1.第一种是res目录下存放的可编译的资源文件:

这种资源文件系统会在R.java里面自动生成该资源文件的ID,所以访问这种资源文件比较简单,通过R.XXX.ID即可;

2.第二种是assets目录下存放的原生资源文件:

因为系统在编译的时候不会编译assets下的资源文件,所以我们不能通过R.XXX.ID的方式访问它们。我们不能通过该资源的绝对路径去访问它们,因为apk安装之后会放在/data/app/**.apk目录下,以apk形式存在,asset/res和被绑定在apk里,并不会解压到/data/data/YourApp目录下去,所以我们无法直接获取到assets的绝对路径,因为它们根本就没有。

不过Android系统提供了一个AssetManager工具类,它提供对应用程序的原始资源文件进行访问;这个类提供了一个低级别的API,它允许以简单的字节流的形式打开和读取和应用程序绑定在一起的原始资源文件。通过getAssets()方法获取AssetManager对象。

1. 先在Activity里面调用getAssets()来获取AssetManager引用。我们首先要做的是把数据库文件“jingdian_db”复制到代码工程的assets文件夹下,assets文件夹里面的文件都是保持原始的文件格式,需要用AssetManager以字节流的形式读取文件。

  2. 再用AssetManager的open(String fileName)方法则指定读取的文件以及访问模式就能得到输入流InputStream。 

3. 然后就是用已经open file 的inputStream读取文件,读取完成后记得inputStream.close() 。

4.调用AssetManager.close()关闭AssetManager。




首先外部导入最主要的是,第一次使用的时候,判断是否已经存在了相应的文件,如果没有,再导入

private void importDB() {
		File file = new File(getFilesDir(), DbName);
		// String DbName = "people_db";
		// 判断是否存在
		if (file.exists() && file.length() > 0) {
			Log.i("import_db", "已存在");
		} else {
			// 使用AssetManager类来访问assets文件夹
			AssetManager asset = getAssets();
			InputStream is = null;
			FileOutputStream fos = null;
			try {
				is = asset.open(DbName);
				fos = new FileOutputStream(file);
				int len = 0;
				byte[] buf = new byte[1024];
				while ((len = is.read(buf)) != -1) {
					fos.write(buf, 0, len);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					if (is != null) {
						is.close();
					}
					if (fos != null) {
						fos.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}



同样,打开数据库文件,不是以前的SQLiteDatabase db = getWritableDatabase()/getReadableDatabase();

而是: SQLiteDatabase sqLite = SQLiteDatabase.openOrCreateDatabase("data/data/com.test.phonebooks/files/people_db", null);

path传值:data/data/包名/files/数据库名称


本次操作的数据库有四个字段,分别是_id,name,age,position

这是创建表stuff时的属性:"create table stuff(_id integer primary key autoincrement, name text not null, age integer, position text not null)"


MainActivity类:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
	// 定义两个按钮
	private Button submit, look;

	static String DbName = "people_db";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// 根据id 获取到相对应的控件
		submit = (Button) findViewById(R.id.button1);
		look = (Button) findViewById(R.id.button2);

		// 按钮设置点击监听
		submit.setOnClickListener(this);
		look.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			importDB();
			break;
		case R.id.button2:
			Intent intent = new Intent();
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			intent.setClass(this, ResultActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}

	}

	private void importDB() {
		File file = new File(getFilesDir(), DbName);
		// String DbName = "people_db";
		// 判断是否存在
		if (file.exists() && file.length() > 0) {
			Log.i("import_db", "已存在");
		} else {
			// 使用AssetManager类来访问assets文件夹
			AssetManager asset = getAssets();
			InputStream is = null;
			FileOutputStream fos = null;
			try {
				is = asset.open(DbName);
				fos = new FileOutputStream(file);
				int len = 0;
				byte[] buf = new byte[1024];
				while ((len = is.read(buf)) != -1) {
					fos.write(buf, 0, len);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					if (is != null) {
						is.close();
					}
					if (fos != null) {
						fos.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}




ResultActivity类:

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.test.mode.Person;

public class ResultActivity extends Activity implements OnItemClickListener {
	private ListView listView;
	private DbAdapter adapter;
	// 打开数据库
	static SQLiteDatabase sqLite = SQLiteDatabase.openOrCreateDatabase(
			"data/data/com.test.phonebooks/files/people_db", null);
	public static final String TABLE_NAME = "stuff";
	List<Person> bookList = new ArrayList<Person>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);

		// 获取ListView
		listView = (ListView) findViewById(R.id.listView1);
		bookList = queryData();
		// 实例化DbAdapter
		adapter = new DbAdapter(getApplication(), bookList);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);

	}

	// 查询数据库,将每一行的数据封装成一个person 对象,然后将对象添加到List中
	private List<Person> queryData() {
		List<Person> list = new ArrayList<Person>();

		// 调用query()获取Cursor
		Cursor c = sqLite.query(TABLE_NAME, null, null, null, null, null, null,
				null);
		while (c.moveToNext()) {
			int _id = c.getInt(c.getColumnIndex("_id"));
			String name = c.getString(c.getColumnIndex("name"));
			int age = c.getInt(c.getColumnIndex("age"));
			String position = c.getString(c.getColumnIndex("position"));

			// 用一个Person对象来封装查询出来的数据
			Person p = new Person();
			p.set_id(_id);
			p.setName(name);
			p.setAge(age);
			p.setPosition(position);

			list.add(p);
		}
		return list;
	}

	// 自定义DbAdapter
	public class DbAdapter extends BaseAdapter {
		private List<Person> list;
		private Context context;
		private LayoutInflater layoutInflater;

		public DbAdapter(Context context, List<Person> list) {
			layoutInflater = LayoutInflater.from(context);
			this.context = context;
			this.list = list;
		}

		// 刷新适配器,更新数据
		public void refresh(List<Person> list) {
			this.list = list;
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Person p = list.get(position);
			ViewHolder holder;

			if (convertView == null) {
				holder = new ViewHolder();
				convertView = layoutInflater.inflate(R.layout.item, null);
				holder.name = (TextView) convertView
						.findViewById(R.id.textView1);
				holder.age = (TextView) convertView
						.findViewById(R.id.textView2);
				holder.position = (TextView) convertView
						.findViewById(R.id.textView3);

				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.name.setText(p.getName());
			holder.age.setText("" + p.getAge());
			holder.position.setText(p.getPosition());

			return convertView;
		}

		public class ViewHolder {
			public TextView name;
			public TextView age;
			public TextView position;
			public TextView id;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view,
			final int position, long id) {
		final Person p = bookList.get(position);
		final long temp = id;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("真的要删除该记录?")
				.setPositiveButton("是", new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// 查询数据库是否是关闭状态
						Boolean bb = sqLite.isOpen();
						if (bb == false) {
							// 如果是关闭状态,就打开数据库,否则会报错
							sqLite = SQLiteDatabase
									.openOrCreateDatabase(
											"data/data/com.test.phonebooks/files/people_db",
											null);
						}
						// 查询完毕后,会关闭数据库,此时操作数据库删除条目,会报错
						/*
						 * 错误信息:java.lang.illegalstateexception
						 * android.database.
						 * sqlite.SQLiteDatabase.acquireference()
						 */
						sqLite.delete(TABLE_NAME, "_id=?",
								new String[] { String.valueOf(p.get_id()) });

						// 重新刷新适配器,并重新查询
						adapter.refresh(queryData());
					}
				}).setNegativeButton("否", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

					}
				}).create().show();

		// 关闭数据库
		sqLite.close();
	}

}



还有一个Person类:

public class Person {
	private String name;
	private int age;
	private String position;
	private int _id;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

	public int get_id() {
		return _id;
	}

	public void set_id(int _id) {
		this._id = _id;
	}

}



源码下载