用加载本地web资源文件暴力获取的方式:

 String path = "file:///android_asset/平舆.tile";

结果在这里不可行,在网上查询了很多资料,思路大致就是先把文件复制到缓存中,然后再获取文件的路径。代码如下所示:"平舆.tile"是assets文件夹下面的文件,我这里的文件路径是:我这里直接是一级目录,如果目录是多个层级,也只要用最终层级就可以了,比如我这里就是平舆.tile

String path=copyAssetGetFilePath("平舆.tile");

获取路径代码

private String copyAssetGetFilePath(String fileName) {
    try {
        File cacheDir = getContext().getCacheDir();
        if (!cacheDir.exists()) {
            cacheDir.mkdirs();
        }
        File outFile = new File(cacheDir, fileName);
        if (!outFile.exists()) {
            boolean res = outFile.createNewFile();
            if (!res) {
                return null;
            }
        } else {
            if (outFile.length() > 10) {//表示已经写入一次
                return outFile.getPath();
            }
        }
        InputStream is = getContext().getAssets().open(fileName);
        FileOutputStream fos = new FileOutputStream(outFile);
        byte[] buffer = new byte[1024];
        int byteCount;
        while ((byteCount = is.read(buffer)) != -1) {
            fos.write(buffer, 0, byteCount);
        }
        fos.flush();
        is.close();
        fos.close();
        return outFile.getPath();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

 

最终可以把path处理成File;

File file = new File(path);

问题总算是解决了。

 

GitHub 加速计划 / ass / assets
29
1
下载
Ultralytics assets
最近提交(Master分支:2 个月前 )
dcececeb Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com> 3 天前
edbe87b8 Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com> Co-authored-by: UltralyticsAssistant <web@ultralytics.com> 11 天前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐