参考链接
-
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();
本文详细介绍了一种从Android资源加载文件并将其缓存至本地的方法。通过使用Context的getCacheDir()和getAssets()方法,可以有效地将assets目录下的文件读取并写入缓存目录,实现资源的高效加载和离线访问。

828

被折叠的 条评论
为什么被折叠?



