文件创建资产android
我正在构建一个应用程序,它的作用是扫描 root
文件夹
然后它搜索里面是否有任何文件夹...这样做的目的是保存所有 数据库中的文本文件路线。数据库将包含文本文件名和路线。
这是代码的一部分:
private void seedData(int indent, File file) throws IOException {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++)
{
seedData(indent + 4, files[i]);
path+=files[i].getPath();
}
}
else{
db.execSQL("insert into "+TABLE+" (title, url) values ('"+
file.getName().substring(0, file.getName().length()-4)+"', '"+file.getPath()+"');");
}
}
但是在普通的Java中,我只是创建一个带有路由的文件,然后将其发送到类似的方法 this: seedData(1, new File("/root"));
。所以我的问题是,如何在 Android 中执行此操作?或者更准确地说,如何创建一个指向位于 assets
中的 root
文件夹的文件,以便它被我的代码“扫描”。我已经尝试过 seedData(1, new File("/assets/root"));
但它不起作用。 任何帮助将不胜感激。
注意:不,我无法手动保存路径,因为所有这些子文件夹中都有超过 3k 的文本文件。
I'm buiding an app that what it does is scan the root
folder
and then it searches if there is any folder inside... the purpose of this is to save all the
text files routes in database..the database will contain the textfile name and the route.
this is part of the code:
private void seedData(int indent, File file) throws IOException {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++)
{
seedData(indent + 4, files[i]);
path+=files[i].getPath();
}
}
else{
db.execSQL("insert into "+TABLE+" (title, url) values ('"+
file.getName().substring(0, file.getName().length()-4)+"', '"+file.getPath()+"');");
}
}
but in normal Java I just create a File with the route and then send it to the method like
this: seedData(1, new File("/root"));
.So my question is, how do I do this in Android? or to be more precise, how do I create a file that points to the root
folder that is located in assets
so it gets "scanned" by my code. I already tried seedData(1, new File("/assets/root"));
but it just didnt work.
Any help would be much appreciated.
Note: No, I cannot save the paths manually since there is over 3k text files in all those subfolders.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是资产不是文件。与上述方法等效的 Android 版本如下所示:
您可以从 Activity 中调用此方法,
作为参考,“root”文件夹中资源的 URI 格式为
file:///android_asset/root/.. .
。The problem here is that the assets are not files. The Android equivalent of your above method would be something like this:
You would call this from your Activity with
For reference, the URI format for assets in the "root" folder is
file:///android_asset/root/...
.