文件创建资产android

发布于 2024-12-25 02:00:23 字数 1087 浏览 1 评论 0原文

我正在构建一个应用程序,它的作用是扫描 root 文件夹

Assets

然后它搜索里面是否有任何文件夹...这样做的目的是保存所有 数据库中的文本文件路线。数据库将包含文本文件名和路线。

这是代码的一部分:

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

Assets

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぺ禁宫浮华殁 2025-01-01 02:00:23

这里的问题是资产不是文件。与上述方法等效的 Android 版本如下所示:

private void seedData(int indent, String path, AssetManager mgr)
    throws IOException
{
    String[] list = mgr.list(path);
    if (list.length > 0) {
        // path is a directory with something in it
        for (String item : list) {
            seedData(indent + 4, path + "/" + item, mgr);
        }
    } else {
        // path is either an empty directory or a file
        // unfortunately, Android makes it hard to distinguish
        String name = path.substring(path.lastIndexOf('/') + 1);
        db.execSQL("insert into "+TABLE+" (title, url) values ('"
                +name.substring(0, name.length()-4)+"', '"
                +path+"');");
    }
}

您可以从 Activity 中调用此方法,

seedData(0, "root", getAssets());

作为参考,“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:

private void seedData(int indent, String path, AssetManager mgr)
    throws IOException
{
    String[] list = mgr.list(path);
    if (list.length > 0) {
        // path is a directory with something in it
        for (String item : list) {
            seedData(indent + 4, path + "/" + item, mgr);
        }
    } else {
        // path is either an empty directory or a file
        // unfortunately, Android makes it hard to distinguish
        String name = path.substring(path.lastIndexOf('/') + 1);
        db.execSQL("insert into "+TABLE+" (title, url) values ('"
                +name.substring(0, name.length()-4)+"', '"
                +path+"');");
    }
}

You would call this from your Activity with

seedData(0, "root", getAssets());

For reference, the URI format for assets in the "root" folder is file:///android_asset/root/....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文