1

我正在构建一个 Android 应用程序,它需要一个空数据库位于 sdcard 的特定文件夹中。因此,我构建了一个代码,从服务器下载数据库并以编程方式插入 sdcard,这工作正常。现在的问题是当我从系统运行应用程序时,每次新数据库都通过替换旧数据库来占用。现在我想更改代码,就好像文件夹中不存在数据库文件然后我只需要下载并保存在特定文件夹中

我的代码在这里:

     String DownloadUrl = "http://myexample.com/android/timeexample.db";
     String fileName = "timeexample.db";

     DownloadDatabase(DownloadUrl,fileName);

public void DownloadDatabase(String DownloadUrl, String fileName) {
    try {
        File root = android.os.Environment.getExternalStorageDirectory();
        File dir = new File(root.getAbsolutePath() + "/timeexample/databases");
        if(dir.exists() == false){
             dir.mkdirs();  
        }
        URL url = new URL("http://myexample.com/android/timeexample.db");
        File file = new File(dir,fileName);

        long startTime = System.currentTimeMillis();
        Log.d("DownloadManager" , "download beginning");
        Log.d("DownloadManager" , "download url:" +url);
        Log.d("DownloadManager" , "download file name:" + fileName);

        URLConnection uconn = url.openConnection();
        uconn.setReadTimeout(TIMEOUT_CONNECTION);
        uconn.setConnectTimeout(TIMEOUT_SOCKET);

        InputStream is = uconn.getInputStream();

        BufferedInputStream bufferinstream = new BufferedInputStream(is);
        ByteArrayBuffer baf = new ByteArrayBuffer(5000);
        int current = 0;
        while((current = bufferinstream.read()) != -1){
            baf.append((byte) current);
        }
        byte[] buffer = new byte[1024];

        FileOutputStream fos = new FileOutputStream( file);
        fos.write(baf.toByteArray());
        fos.flush();
        fos.close();
        Log.d("DownloadManager" , "download ready in" + ((System.currentTimeMillis() - startTime)/1000) + "sec");

    }

    catch(IOException e) {
        Log.d("DownloadManager" , "Error:" + e);
        e.printStackTrace();
    }   
}

如果每次我这样运行,我的数据都会丢失,因为空数据库正在占用。因此我的要求是如果数据库已经存在,则无需下载。我参考了很多例子来解决这个问题。

4

3 回答 3

1

当目录已经存在时,您需要从函数返回

这是你的代码

public void DownloadDatabase(String DownloadUrl, String fileName) {
try {
    File root = android.os.Environment.getExternalStorageDirectory();
    File dir = new File(root.getAbsolutePath() + "/timeexample/databases");
    if(dir.exists() == false){
         dir.mkdirs();  
    }

URL url = new URL("http://myexample.com/android/timeexample.db");
    File file = new File(dir,fileName);

您需要将其更改为

public void DownloadDatabase(String DownloadUrl, String fileName) {
try {
    File root = android.os.Environment.getExternalStorageDirectory();
    File dir = new File(root.getAbsolutePath() + "/timeexample/databases");
    if(dir.exists() == false){
         dir.mkdirs();  
    }
    else 
      return 

URL url = new URL("http://myexample.com/android/timeexample.db");
    File file = new File(dir,fileName);
于 2013-04-25T09:02:44.590 回答
1

尝试这个:

File extStore = Environment.getExternalStorageDirectory();
File myFile = new File(extStore.getAbsolutePath() + "/book1/page2.db");

if(myFile.exists()){
...
}
于 2013-04-25T07:35:47.210 回答
0

检查数据库文件是否存在

File f = new File(Environment.getExternalStorageDirectory()+"/timeexample/databases/timeexample.db");
   if(f.exists())
   { 
     /* do whatever you want */ 
   }

现在,Android 设备中有两种类型的存储可供您使用

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {
    // We can read and write the media
    mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    // We can only read the media
    mExternalStorageAvailable = true;
    mExternalStorageWriteable = false;
} else {
    // Something else is wrong. It may be one of many other states, but all we need
    //  to know is we can neither read nor write
    mExternalStorageAvailable = mExternalStorageWriteable = false;
}
于 2013-04-25T07:31:13.217 回答