2011-12-16 89 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()); 
    } 

} 

够了;

相关问题