android读取已有的sqlite数据库到sdcard或ro

来源:互联网 发布:手机淘宝首页尺寸大小 编辑:程序博客网 时间:2024/05/22 16:53

package com.example.gdpuschedule;

import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import android.content.Context;import android.os.Environment;import android.util.Log;//此类获取sd卡路径!public class db_sacard_helper {public static final String DB_DIR = Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator+ "GDPUkebiao"; // + File.separator ;// + db_sacard_helper.class.getPackage().getName();public static String db_path = DB_DIR + File.separator + "mydb.db";private void writedb(Context context) throws IOException {// Log.i("sss", "文件开始创建" + dbsFile.mkdirs());// 判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库InputStream is = context.getResources().openRawResource(R.raw.mydb);// 输入的流FileOutputStream fos = new FileOutputStream(db_path);// 输出的流byte[] buffer = new byte[5000];int count = 0;while ((count = is.read(buffer)) > 0) {fos.write(buffer, 0, count);}fos.close();is.close();}public db_sacard_helper(Context context) throws IOException {if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {// 可读取sdcard// 创建目录File dbFolder = new File(DB_DIR);if (!dbFolder.exists()) {dbFolder.mkdirs();}// 创建文件File dbsFile = new File(db_path);if (!dbsFile.exists()) {writedb(context);Log.i("sss", "write db in sdcard");}else {Log.i("sss", "db in sdcard");}} else {// 不可读取sdcarddb_path = "/data"+ Environment.getDataDirectory().getAbsolutePath()+ File.separator+ db_sacard_helper.class.getPackage().getName()+ File.separator + "mydb.db";// 创建文件Log.i("sss", "db path::"+db_path);File dbsFile = new File(db_path);if (!dbsFile.exists()) {writedb(context);Log.i("sss", "create db in nei cun");}else {Log.i("sss", "db nei cun youle");}}}}

                                             
0 0
原创粉丝点击