Android:将外部文件链接到 Eclipse 项目
我使用的应用程序有一个外部 SQLite 数据库(位于资产文件夹中)。我的朋友在 iPhone 版本的应用程序中使用相同的数据库文件。 DB文件的内容不断更新。由于这两个项目都位于同一个存储库中,因此我们创建了一个共享文件夹,在其中保存数据库文件,以便两个项目都可以链接到该共享资源。它在 iPhone 项目中有效,但在 Android 中失败。
在 Eclipse 中,我单击 assets/new/file,然后单击 Advanced,然后单击 Link to file in the file system。该文件出现在资产文件夹(在 Eclipse 中)中,但我无法从 JAVA 代码访问它。
为什么这不起作用?是否还有其他方法可以将外部文件链接到 Eclipse 中的项目?
谢谢
编辑:
我使用此代码打开资产文件:
OutputStream os = new FileOutputStream(db_path + DB_NAME);
byte []b = new byte[1024];
int i, r;
//load list of files from 'data' folder
String[] fileCollection = am.list("data");
Arrays.sort(fileCollection);
for(i=0;i<fileCollection.length;i++)
{
//String fn = String.format(DB_NAME"%dd.db", (i + 1));
String fn = DB_NAME + "." + (i + 1);
if(fileCollection[i].equals(fn) == false){
break;
}
InputStream is = am.open("data/"+fn);
while((r = is.read(b)) != -1) {
os.write(b, 0, r);
}
is.close();
}
os.close();
The application that I work on has a external SQLite database (sitting in the assets folder). My friend is using the same DB file in the iPhone version of the app. Content of the DB file is updated continuously. As both projects are in the same repository we created a Shared folder where we keep the DB file so both of use can link to that shared resource. It works in the iPhone project but fails in Android.
When in Eclipse, I click on the assets/new/file and click on Advanced and then Link to file in the file system. The file appears in the assets folder (in Eclipse) but I cannot access it from JAVA code.
Why that doesn't work? Is there any other was of linking external files to the project in Eclipse?
Thanks
EDITED:
I use this code for opening the assets file:
OutputStream os = new FileOutputStream(db_path + DB_NAME);
byte []b = new byte[1024];
int i, r;
//load list of files from 'data' folder
String[] fileCollection = am.list("data");
Arrays.sort(fileCollection);
for(i=0;i<fileCollection.length;i++)
{
//String fn = String.format(DB_NAME"%dd.db", (i + 1));
String fn = DB_NAME + "." + (i + 1);
if(fileCollection[i].equals(fn) == false){
break;
}
InputStream is = am.open("data/"+fn);
while((r = is.read(b)) != -1) {
os.write(b, 0, r);
}
is.close();
}
os.close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您给出的详细信息,我得出结论,您不能直接从资产目录使用数据库文件,您必须将该数据库文件复制到应用程序的内部存储
data/data/database/
中,然后使用它。编辑:
我认为Android环境无法识别系统文件的物理路径,因此当我们尝试链接Android项目中的资产或任何文件夹的任何文件时那么它就无法找到从系统路径链接的文件。
希望我在这一点上没有错。如果是,请让我知道这个主题。
谢谢。
From, the detail you given in question, I conclude that you can not use database file directly from the asset directory, you have to copy that database file into application's internal storage
data/data/database/
and then use it.EDIT:
I think Android environment can't recognize the physical path of you system files, so when we try to link any file for asset or any folder which are in android project hierarchy then it can not find the file which one linked from a system path.
Hope I am not wrong in this. If yes then let me know on this topic.
Thanks.