当设备连接时,如何从DDMS中提取数据库文件?我知道如何在Android模拟器中运行时拉。但我想在设备连接时拉它吗?请帮助解决这个问题。
答案 0 :(得分:0)
我想我理解你的问题,你问的是从android设备中获取数据库的路径。
我们无法获得某些设备数据库,所以我认为可能的解决方案是在项目中创建一个备份按钮,从app的私人文件夹中复制数据库并将其粘贴到SD卡。然后你可以使用sqlite浏览器访问数据库。
以下是复制数据库的代码:
// method to get backup
public void BackupDatabase() throws IOException
{
try {
File sd = new File(Environment.getExternalStorageDirectory()+"");//getBaseContext().getFilesDir();
File data = Environment.getDataDirectory();
if(sd == null || !sd.canWrite() ){
sd = getBaseContext().getFilesDir();
}
if (sd.canWrite())
{
String currentDBPath = "/data/your.package.name/databases/YourDatabaseName";
File temp = new File(sd, "BackupFolderInSDCard");
if(!temp.exists())
temp.mkdirs();
File backupDB = new File(temp,"backup.db");
File currentDB = new File(data, currentDBPath);
if (currentDB.exists())
{
FileInputStream fis = new FileInputStream(currentDB);
BufferedInputStream bis = new BufferedInputStream(fis,1024);
FileOutputStream fos = new FileOutputStream(backupDB);
BufferedOutputStream bos = new BufferedOutputStream(fos, 1024);
byte arr[] = new byte[1024];
int ch;
while( (ch = bis.read(arr)) != -1 )
{
bos.write(arr, 0, ch);
}
bos.close(); fos.close();
bis.close(); fis.close();
// Toast.makeText(getBaseContext(), "File Saved To\n"+ backupDB.getAbsolutePath(), Toast.LENGTH_LONG).show();
Toast.makeText(getBaseContext(), "Database Stored in SD Card", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getBaseContext(), "Database Doesn't Exist"+ backupDB.getAbsolutePath(), Toast.LENGTH_LONG).show();
}