我正在通过此代码获取数据库文件
File dbFile=getDatabasePath("EdsysEyfsDB.db");
Log.v("database name checking", dbFile.toString());
我想将此数据库文件复制到sdcard,以便为此做一些操作。但我无法对此进行任何操作。以下代码用于复制到SD卡
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();
}
但是我不会遇到这种情况。数据库文件名在LogCat上正确显示。我已经授予读写文件的权限。
试试这个希望对您有帮助
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");
注意 :
请记住使用添加写入外部存储的权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
,否则sd.canWrite()将为false。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句