Android 导出资产文件夹中的sqlite数据库
我的资产文件夹中有一个sqlite数据库。我想导出那个文件。所以我这样做:Android 导出资产文件夹中的sqlite数据库,android,database,sqlite,Android,Database,Sqlite,我的资产文件夹中有一个sqlite数据库。我想导出那个文件。所以我这样做: StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .permitAll().build(); StrictMode.setThreadPolicy(policy); // File direct = new File(Environment.getExternalStorageDir
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
//
File direct = new File(Environment.getExternalStorageDirectory()
+ "/Exam Creator");
if (!direct.exists())
{
if (direct.mkdir())
{
// directory is created;
}
}
exportDB();
// importDB();
}
private void exportDB()
{
// TODO Auto-generated method stub
try
{
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite())
{
dbopenhelper.copyDataBase();
String currentDBPath = dbopenhelper.DB_PATH
+ dbopenhelper.DATABASE_NAME;
String backupDBPath = "/BackupFolder/Prayers.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getBaseContext(),
backupDB.toString() + "SUCCESS", Toast.LENGTH_LONG)
.show();
}
} catch (Exception e)
{
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
.show();
}
}
为了获得currentDB的路径,我首先在我的DBOpenHelper中使用这个方法复制项目中的数据库,并在export方法中调用它,如您所见
public static void copyDataBase()
{
try
{
InputStream input = context.getAssets().open(DATABASE_NAME);
FileOutputStream output = new FileOutputStream(DB_PATH
+ DATABASE_NAME);
byte[] data = new byte[1024];
int buffer;
while ((buffer = input.read(data)) != -1)
{
output.write(data, 0, buffer);
}
output.flush();
output.close();
input.close();
} catch (IOException ex)
{
Log.e(LOGTAG, "Error in copying database...");
}
}
但当我运行它时,我得到了FileNotFoundException。我对它进行了调试,发现在这一行之后:
FileChannel src = new FileInputStream(currentDB).getChannel();
它去接布洛克。有人知道怎么回事吗
提前感谢您是否设置了在清单中写入外部存储的权限?请记录并检查您复制数据库的路径frmo和应保存数据库的目录文件路径。输出是什么?请在此处复制并粘贴…@JustinMorrisThanks@Opiatefuchs谢谢,问题解决了:)