「Android/DiskLruCache」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→downloadしたものをcacheする) |
|||
(同じ利用者による、間の11版が非表示) | |||
行17: | 行17: | ||
mopub | mopub | ||
https://github.com/mopub/mopub-android-sdk/blob/master/mopub-sdk/mopub-sdk-base/src/main/java/com/mopub/common/DiskLruCache.java | https://github.com/mopub/mopub-android-sdk/blob/master/mopub-sdk/mopub-sdk-base/src/main/java/com/mopub/common/DiskLruCache.java | ||
+ | |||
+ | ==準備== | ||
+ | key部分のhogehogeはmd5やsha1などでhash化して使うと良い。 | ||
+ | |||
+ | [[android/hash]] [ショートカット] | ||
+ | |||
==使い方サンプル== | ==使い方サンプル== | ||
<pre> | <pre> | ||
+ | String UNIQUE_CACHE_NAME = "hogehoge-cache"; | ||
+ | int VALUE_COUNT = 1; | ||
+ | int DISK_CACHE_INDEX = 0; | ||
+ | String cachePath = context.getCacheDir().getPath() | ||
+ | File cacheDirectory = new File(cachePath + File.separator + UNIQUE_CACHE_NAME); | ||
+ | long diskCacheSizeBytes = 30 * 1024 * 1024; // 30 MB | ||
+ | // 初期化 | ||
+ | DiskLruCache sDiskLruCache = DiskLruCache.open( | ||
+ | cacheDirectory, | ||
+ | APP_VERSION, | ||
+ | VALUE_COUNT, | ||
+ | diskCacheSizeBytes); | ||
+ | // 取得 | ||
+ | DiskLruCache.Snapshot snapshot = sDiskLruCache.get("hogehoge"); | ||
+ | final InputStream in = snapshot.getInputStream(DISK_CACHE_INDEX); | ||
+ | // 更新 | ||
+ | DiskLruCache.Editor editor = sDiskLruCache.edit("hogehoge"); | ||
+ | final OutputStream outputStream = | ||
+ | new BufferedOutputStream(editor.newOutputStream(DISK_CACHE_INDEX)); | ||
+ | Streams.copyContent(content, outputStream); | ||
+ | outputStream.flush(); | ||
+ | outputStream.close(); | ||
+ | sDiskLruCache.flush(); | ||
+ | editor.commit(); | ||
+ | |||
+ | // 削除 | ||
+ | sDiskLruCache.delete(); | ||
+ | |||
+ | public class Streams { | ||
+ | public static void copyContent(final InputStream inputStream, final OutputStream outputStream) throws IOException { | ||
+ | if (inputStream == null || outputStream == null) { | ||
+ | throw new IOException("Unable to copy from or to a null stream."); | ||
+ | } | ||
+ | byte[] buffer = new byte[16384]; | ||
+ | int length; | ||
+ | while ((length = inputStream.read(buffer)) != -1) { | ||
+ | outputStream.write(buffer, 0, length); | ||
+ | } | ||
+ | } | ||
+ | } | ||
</pre> | </pre> | ||
− | + | mopubに上記コードの詳細がある | |
https://github.com/mopub/mopub-android-sdk/blob/master/mopub-sdk/mopub-sdk-base/src/main/java/com/mopub/common/CacheService.java | https://github.com/mopub/mopub-android-sdk/blob/master/mopub-sdk/mopub-sdk-base/src/main/java/com/mopub/common/CacheService.java | ||
+ | |||
+ | ==downloadしたものをcacheする== | ||
+ | <pre> | ||
+ | URL u = new URL(mUrl); | ||
+ | InputStream is = u.openStream(); | ||
+ | HttpURLConnection huc = (HttpURLConnection)u.openConnection(); | ||
+ | if (huc != null) { | ||
+ | InputStream inputStream = new BufferedInputStream(huc.getInputStream()); | ||
+ | boolean diskPutResult = CacheService.putToDiskCache(mUrl, inputStream); | ||
+ | } | ||
+ | </pre> | ||
==permission追加== | ==permission追加== | ||
− | context.getExternalCacheDir() | + | 保存場所はconstructのparamで指定するので、適宜記述となる。 |
+ | |||
+ | mopubではcontext.getCacheDir()を使っておりAndroidManifestのpermissionは不要だった。 | ||
+ | https://github.com/mopub/mopub-android-sdk/blob/master/mopub-sdk/mopub-sdk-base/src/main/java/com/mopub/common/CacheService.java | ||
+ | |||
+ | context.getExternalCacheDir()をもし使う場合はKitKatからpermission不要となったが、それ以前では必要なので以下のように追加する | ||
<pre> | <pre> | ||
<uses-permission | <uses-permission | ||
行34: | 行96: | ||
http://wada811.blogspot.com/2014/09/storage-access-and-permission-in-android.html | http://wada811.blogspot.com/2014/09/storage-access-and-permission-in-android.html | ||
+ | |||
+ | https://developer.android.com/training/data-storage/files |
2018年11月29日 (木) 17:16時点における最新版
準備
以下追加
- app/build.gradle
dependencies { implementation 'com.jakewharton:disklrucache:2.0.2' }
公式?
https://github.com/JakeWharton/DiskLruCache
準備
key部分のhogehogeはmd5やsha1などでhash化して使うと良い。
android/hash [ショートカット]
使い方サンプル
String UNIQUE_CACHE_NAME = "hogehoge-cache"; int VALUE_COUNT = 1; int DISK_CACHE_INDEX = 0; String cachePath = context.getCacheDir().getPath() File cacheDirectory = new File(cachePath + File.separator + UNIQUE_CACHE_NAME); long diskCacheSizeBytes = 30 * 1024 * 1024; // 30 MB // 初期化 DiskLruCache sDiskLruCache = DiskLruCache.open( cacheDirectory, APP_VERSION, VALUE_COUNT, diskCacheSizeBytes); // 取得 DiskLruCache.Snapshot snapshot = sDiskLruCache.get("hogehoge"); final InputStream in = snapshot.getInputStream(DISK_CACHE_INDEX); // 更新 DiskLruCache.Editor editor = sDiskLruCache.edit("hogehoge"); final OutputStream outputStream = new BufferedOutputStream(editor.newOutputStream(DISK_CACHE_INDEX)); Streams.copyContent(content, outputStream); outputStream.flush(); outputStream.close(); sDiskLruCache.flush(); editor.commit(); // 削除 sDiskLruCache.delete(); public class Streams { public static void copyContent(final InputStream inputStream, final OutputStream outputStream) throws IOException { if (inputStream == null || outputStream == null) { throw new IOException("Unable to copy from or to a null stream."); } byte[] buffer = new byte[16384]; int length; while ((length = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, length); } } }
mopubに上記コードの詳細がある https://github.com/mopub/mopub-android-sdk/blob/master/mopub-sdk/mopub-sdk-base/src/main/java/com/mopub/common/CacheService.java
downloadしたものをcacheする
URL u = new URL(mUrl); InputStream is = u.openStream(); HttpURLConnection huc = (HttpURLConnection)u.openConnection(); if (huc != null) { InputStream inputStream = new BufferedInputStream(huc.getInputStream()); boolean diskPutResult = CacheService.putToDiskCache(mUrl, inputStream); }
permission追加
保存場所はconstructのparamで指定するので、適宜記述となる。
mopubではcontext.getCacheDir()を使っておりAndroidManifestのpermissionは不要だった。 https://github.com/mopub/mopub-android-sdk/blob/master/mopub-sdk/mopub-sdk-base/src/main/java/com/mopub/common/CacheService.java
context.getExternalCacheDir()をもし使う場合はKitKatからpermission不要となったが、それ以前では必要なので以下のように追加する
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />
http://wada811.blogspot.com/2014/09/storage-access-and-permission-in-android.html