美文网首页
Android 如何获取 assets 下文件的路径

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

作者: Cooliean | 来源:发表于2020-03-27 14:58 被阅读0次

因为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");

相关文章

网友评论

      本文标题:Android 如何获取 assets 下文件的路径

      本文链接:https://www.haomeiwen.com/subject/qpbluhtx.html