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 { // 不可读取sdcard db_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"); } } } }
android读取已有的sqlite数据库到sdcard或ro
最新推荐文章于 2018-11-06 18:17:25 发布