diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_TileManager.java b/mobile/src/com/cradle/iitc_mobile/IITC_TileManager.java index ed171087..c0795a92 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_TileManager.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_TileManager.java @@ -39,25 +39,17 @@ public class IITC_TileManager { String path = mIitc.getApplication().getFilesDir().toString() + "/" + hostId + filePath; if (uri.getQuery() != null) path += uri.getQuery(); - /* - * unfortunately, the filename is included in our path. since we may need to create directories, - * we need filename and filepath split. - */ - String[] split = path.split("/"); - String fileName = split[split.length - 1]; - path = path.replace(fileName, ""); - // do the tile management - File file = new File(path, fileName); + File file = new File(path); if (file.exists()) { // asynchronously download tile if outdated, ignore date if not connected to wifi - if (mIitc.getWebView().isConnectedToWifi()) new DownloadTile(path, fileName).execute(url); + if (mIitc.getWebView().isConnectedToWifi()) new DownloadTile(path).execute(url); // return tile from storage InputStream in = new BufferedInputStream(new FileInputStream(file)); return new WebResourceResponse(TYPE, ENCODING, in); } else { // asynchronously download tile to cache and let webviewclient load the resource - new DownloadTile(path, fileName).execute(url); + new DownloadTile(path).execute(url); return null; } } diff --git a/mobile/src/com/cradle/iitc_mobile/async/DownloadTile.java b/mobile/src/com/cradle/iitc_mobile/async/DownloadTile.java index 02968bae..8646f880 100644 --- a/mobile/src/com/cradle/iitc_mobile/async/DownloadTile.java +++ b/mobile/src/com/cradle/iitc_mobile/async/DownloadTile.java @@ -13,11 +13,9 @@ import java.net.URLConnection; public class DownloadTile extends AsyncTask { private String mFilePath; - private String mFileName; - public DownloadTile(String path, String fileName) { + public DownloadTile(String path) { mFilePath = path; - mFileName = fileName; } @@ -28,13 +26,13 @@ public class DownloadTile extends AsyncTask { try { tileUrl = new URL(urls[0]); conn = tileUrl.openConnection(); - File file = new File(mFilePath, mFileName); + File file = new File(mFilePath); // update tile if needed, else return if (conn.getLastModified() < file.lastModified()) return true; InputStream is = null; is = conn.getInputStream(); Log.d("iitcm", "writing to file: " + file.toString()); - writeTileToFile(is, file, mFilePath); + writeTileToFile(is, file); } catch (IOException e) { e.printStackTrace(); return false; @@ -45,9 +43,8 @@ public class DownloadTile extends AsyncTask { return true; } - private void writeTileToFile(InputStream inStream, File file, String path) throws Exception { - File filePath = new File(path); - filePath.mkdirs(); + private void writeTileToFile(InputStream inStream, File file) throws Exception { + file.getParentFile().mkdirs(); FileOutputStream outStream = new FileOutputStream(file); int bufferSize = 1024; byte[] buffer = new byte[bufferSize];