小编Gen*_*kin的帖子

Context.openFileInput和Context.openFileOutput之间的同步

我有一个Service每天运行的Android ,可以进行一些数据同步.每天一次下载文件并通过context.openFileOutput以下方式将其缓存到磁盘:

String fileName = Uri.parse(url).getLastPathSegment();
try (FileOutputStream outputStream =
     context.openFileOutput(fileName, Context.MODE_PRIVATE)) {
   outputStream.write(bytes);
   // ...
} catch (IOException e) {
   // logging ...
}
Run Code Online (Sandbox Code Playgroud)

这发生在后台线程上.我也有一个包含一个的UI WebView.在WebView使用它们是否可用通过这些缓存的资源context.openFileInput:

@Override
public WebResourceResponse shouldInterceptRequest(
    WebView view, WebResourceRequest request) {
  String url = request.getUrl().toString();
  if (shouldUseCache(url)) {
     try {
        return new WebResourceResponse(
             "video/webm",
             Charsets.UTF_8.name(),
             context.openFileInput(obtainFileName(url)));
     } catch (IOException e) {
       // If a cached resource fails to load, just let the WebView load …
Run Code Online (Sandbox Code Playgroud)

java android synchronization caching android-webview

7
推荐指数
1
解决办法
199
查看次数