拷贝数据库文件在android的SD卡 [英] copy database file to sdcard in android
问题描述
我对这个code让我的数据库文件
I am getting my database file by this code
File dbFile=getDatabasePath("EdsysEyfsDB.db");
Log.v("database name checking", dbFile.toString());
我要复制这个数据库文件到SD卡,所以我可以做一些操作的。但我不能做任何操作。下面code使用拷贝到SD卡
I want to copy this database file to sdcard so I can do some operation for that. But I can't do any operation on that. The below code is using for copy to sd card
if (dbFile.exists()) {
InputStream inStream = new FileInputStream(dbFile);
String file = Environment.getExternalStorageDirectory().getPath()
+"/" + "database.db";
Log.d("file name checking in dbFilecondition", file);
FileOutputStream fs = new FileOutputStream(file);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
fs.write(buffer, 0, byteread);
}
inStream.close();
fs.close();
}
不过,我不打算在此condition.The数据库文件名上LogCat中来了正常。我已经给权限读取和写入文件。
But I am not going in this condition.The database file name is coming properly on LogCat. I already give permission for Read and Write file.
推荐答案
试试这个希望这可以帮助你。
Try this hope this helps you
public void exportDatabse(String databaseName) {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//"+getPackageName()+"//databases//"+databaseName+"";
String backupDBPath = "backupname.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Exception e) {
}
}
如何调用
exportDatabse("YourDBName");
注意:
记住要添加权限写入到外部存储与
<使用-权限的Android:名称=android.permission.WRITE_EXTERNAL_STORAGE/>
,否则sd.canWrite()将是错误的。
Remember to add permission to write to external storage with
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
, otherwise sd.canWrite() will be false.
这篇关于拷贝数据库文件在android的SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!