我的应用程序备份数据库有问题。我正在使用Android2.2.3,这已经安装了sd卡。制作数据库的副本很好。这个问题发生在我用内存卡(Nexus32GB)这样的内存在手机上测试我的应用程序时。在这个场景中,我的方法无法将文件提取到sd卡,因为它不存在(sd卡)。如何将数据库复制到内部独立的位置?我试过:
File outPut = new File(Environment.getRootDirectory().toString() + "/myfolder/");但获得的权限被拒绝,无法使用数据创建文件夹。有人能显示正确的方式吗?
编辑:
我还是不明白。我想用dataBackup制作新文件夹。我已经为它定义了正确的位置,但它说找不到文件。SDCard出现了。为什么不能创建该文件夹-“/storeUGif/databaseName.db”?
以下是目标文件夹的绝对路径:
public static final String OUTPUT_BACKUP_DATABASE = Environment.getExternalStorageDirectory().getAbsolutePath() + "/storeUGif/" + SqliteHelper.DATABASE_NAME;
if(isSdPresent())
{
//File outPut = new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+"/StoreUGif/"+SqliteHelper.DATABASE_NAME);
File outPut = new File(Tools.OUTPUT_BACKUP_DATABASE);
File storeUGif = new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString());
storeUGif.mkdir();
File currentDB = getApplicationContext().getDatabasePath(SqliteHelper.DATABASE_NAME);
FileInputStream is = new FileInputStream(currentDB);
OutputStream os = new FileOutputStream(outPut);
byte[] buffer = new byte[1024];
while (is.read(buffer) > 0) {
os.write(buffer);
}
os.flush();
os.close();
is.close();
}
else
{
Toast.makeText(this, "SDCard is unvailable", Toast.LENGTH_SHORT).show();
}发布于 2014-05-16 13:19:31
使用Environment.getExternalStorageDirectory()获取有效路径。
尽管它的名称,它将返回默认存储,或外部或(如果发送)内部存储。
参考:http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()
https://stackoverflow.com/questions/23695864
复制相似问题