2013-04-25 136 views
1

我正在構建一個android應用程序,它需要一個空數據庫才能在sdcard的特定文件夾中。因此,我建立一個代碼作爲從服務器下載數據庫,並以編程方式插入SD卡,它工作正常。現在的問題是,當我從系統運行應用程序時,每次新數據庫都是通過替換舊數據庫來完成的。現在我想,如果數據庫文件未在文件夾中存在的則只有我需要下載並保存在特定的文件夾Android:檢查文件是否存在於文件夾中或不在SD卡中

和我的代碼是在這裏不用

 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(); 
    } 
} 

如果每次我會像這樣改變代碼,我的數據是空的數據庫正在採取。因此我的要求是如果數據庫已經存在,那麼不需要下載。引用很多例子來解決這個問題。任何人都可以幫助我解決問題..在此先感謝。

回答

0

用於檢查數據庫文件中存在或不使用

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

現在天就有兩頁式存儲在Android設備上可用的,我會sagest您使用

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; 
} 
1

試試這個:

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

if(myFile.exists()){ 
... 
} 
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);