Android 如何获取 assets 下文件的路径

因为Android 放在assets中的文件,是再apk里面的。webView哪些可以直接访问一些资源文件,html等。
但是某些文件,不能直接再app中使用,只能复制出来使用,

下面是复制方法

通过文件读写的方式,写到缓存文件里面去,通过获取缓存文件路径 来读取,见如下代码 ,byte的size根据自己文件大小来确认,可自己调整

public String getAssetsCacheFile(Context context,String fileName)   {
        File cacheFile = new File(context.getCacheDir(), fileName);
        try {
            InputStream inputStream = context.getAssets().open(fileName);
            try {
                FileOutputStream outputStream = new FileOutputStream(cacheFile);
                try {
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = inputStream.read(buf)) > 0) {
                        outputStream.write(buf, 0, len);
                    }
                } finally {
                    outputStream.close();
                }
            } finally {
                inputStream.close();
            }
        } catch (IOException e) {
           e.printStackTrace();
        }
        return cacheFile.getAbsolutePath();
    }

String path = getAssetsCacheFile(mContext, "xxx.zip");
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容