2011-12-16 101 views
0

我正在開發以數據庫作爲後端的黑莓手機應用程序。數據庫有一些數據,所以我將數據庫從res導入到sdcard,它在模擬器中正常工作。 當我安裝my app into device那麼它不工作可能是我無法理解的問題。下面是我的代碼..如何將數據庫複製到黑莓手機中的設備

調用方法

DatabseCopy db=new DatabseCopy(); 
     db.copyFile("/nm.db","file:///SDCard/Databases/nm.db");  

方法

public void copyFile(String srFile, String dtFile) 
    { 
     try 
     {     
       FileConnection fconn;      
       fconn = (FileConnection) Connector.open(dtFile,Connector.READ_WRITE); 

       if(!fconn.exists()) // if file does not exists , create a new one 
       { 
         fconn.create(); 
       } 

       InputStream is = (InputStream)this.getClass().getResourceAsStream(srFile); 
       OutputStream os =fconn.openOutputStream(); 
       byte[] buf = new byte[1024]; 
       int len; 
       while ((len = is.read(buf)) > 0) 
       { 
       os.write(buf, 0, len); 
       } 
      is.close(); 
      os.close(); 
     } 
     catch(IOException e) 
     { 
      System.out.println("Exception"+e.getMessage())   ; 
     } 
} 
+0

你得到了哪個錯誤?如果你想更多來這裏http://chat.stackoverflow.com/rooms/4014/knowledge-sharing-center-for-blackberry-and-java – 2011-12-16 07:51:22

+0

更好地改變標題 – alishaik786 2011-12-16 12:14:15

回答

1

試試這個: 試圖在此之前:你必須檢查SD卡是有或不和

System.getProperty( 「fileconn.dir.memorycard」)

直接給出路徑高達:

文件:/// SD卡/

,然後你的文件名;

private void copyFromResToSDCard() 
{  
    try 
    { 
     InputStream is=(InputStream)getClass().getResourceAsStream("/ManualRecords.db"); 
     FileConnection fileconn=(FileConnection)Connector.open(System.getProperty("fileconn.dir.memorycard")+"ManualRecords.db");//Here set your Path with new fileName.db; 
     if(fileconn.exists()) 
     { 
      fileconn.delete();    
     } 
     fileconn.create(); 
     byte data[]=new byte[is.available()]; 
     data=IOUtilities.streamToBytes(is); 
     OutputStream os=fileconn.openOutputStream(); 
     os.write(data); 
     fileconn.close(); 
     is.close(); 
     os.close(); 
    } 
    catch (Exception e) 
    { 
     System.out.println("=============="+e.getMessage()); 
    } 

} 

夠了;

相關問題