質問編集履歴
2
誤字修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -15,7 +15,9 @@
|
|
15
15
|
以下の記事を参考にコードを書いていたところ、
|
16
16
|
|
17
17
|
https://qiita.com/chouxcreams/items/abb032d8127098dea7d1#%E7%94%BB%E5%83%8F%E3%82%92%E3%83%80%E3%82%A6%E3%83%B3%E3%83%AD%E3%83%BC%E3%83%89%E3%81%99%E3%82%8B
|
18
|
+
```
|
18
|
-
|
19
|
+
val (_, _, result) = url.httpGet().awaitByteArrayResponseResult()
|
20
|
+
```
|
19
21
|
で
|
20
22
|
`java.lang.OutOfMemoryError: Failed to allocate a 892384224 byte allocation with 1556480 free bytes and 510MB until OOM, target footprint 3018616, growth limit 536870912`
|
21
23
|
と表示され、実行できませんでした。
|
1
試したことを追記
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
Android アプリで OutOfMemoryError を発生させずにファイルをダウンロード
|
1
|
+
Android アプリで OutOfMemoryError を発生させずに大きなファイルをダウンロードしたい
|
body
CHANGED
@@ -1,13 +1,53 @@
|
|
1
|
-
## 実現したいこと
|
1
|
+
## 前提・実現したいこと
|
2
2
|
|
3
|
-
|
3
|
+
### 環境
|
4
4
|
|
5
|
+
* Windows
|
5
|
-
|
6
|
+
* Kotlin
|
7
|
+
* Android Studio
|
6
8
|
|
9
|
+
### 実現したいこと
|
10
|
+
|
11
|
+
Android アプリで 700 MB のファイルをインターネットからアプリのインストールされている端末にダウンロードしたい
|
12
|
+
|
13
|
+
## 試したこと
|
14
|
+
|
7
15
|
以下の記事を参考にコードを書いていたところ、
|
8
16
|
|
9
17
|
https://qiita.com/chouxcreams/items/abb032d8127098dea7d1#%E7%94%BB%E5%83%8F%E3%82%92%E3%83%80%E3%82%A6%E3%83%B3%E3%83%AD%E3%83%BC%E3%83%89%E3%81%99%E3%82%8B
|
18
|
+
`val (_, _, result) = url.httpGet().awaitByteArrayResponseResult()`
|
19
|
+
で
|
20
|
+
`java.lang.OutOfMemoryError: Failed to allocate a 892384224 byte allocation with 1556480 free bytes and 510MB until OOM, target footprint 3018616, growth limit 536870912`
|
21
|
+
と表示され、実行できませんでした。
|
10
22
|
|
23
|
+
次に https://github.com/kittinunf/Fuel のテストコードを参考に
|
24
|
+
以下のコードを書いたところ
|
25
|
+
```
|
11
|
-
|
26
|
+
val (request, response, result) = Fuel.download(url)
|
27
|
+
.fileDestination { _, _ -> File(path + "/" + File) }
|
28
|
+
.progress { _, _ -> progressCalls += 1 }
|
29
|
+
.responseString()
|
30
|
+
val (data, error) = result
|
31
|
+
```
|
12
32
|
|
33
|
+
`android.os.NetworkOnMainThreadException` というエラーで実行できません。
|
34
|
+
|
35
|
+
上のエラー解消には Coroutine を使うということが分かったので、以下のコードを追加したのですが、
|
36
|
+
```
|
37
|
+
val downloader: myDownloader = MyDownloader()
|
38
|
+
val path: String? = Environment.getExternalStorageDirectory().getAbsolutePath();
|
39
|
+
val scope = CoroutineScope(Dispatchers.Default)
|
40
|
+
scope.launch {
|
41
|
+
downloader.download(path, "700MBファイルのURL")
|
42
|
+
}
|
43
|
+
```
|
44
|
+
やはり以下で
|
45
|
+
```
|
46
|
+
val (request, response, result) = Fuel.download(url)
|
47
|
+
.fileDestination { _, _ -> File(path + "/" + File) }
|
48
|
+
.progress { _, _ -> progressCalls += 1 }
|
49
|
+
.responseString()
|
50
|
+
val (data, error) = result
|
51
|
+
```
|
13
|
-
|
52
|
+
`java.lang.OutOfMemoryError: Failed to allocate a 892384224 byte allocation with 1556480 free bytes and 510MB until OOM, target footprint 3018616, growth limit 536870912`
|
53
|
+
と表示され、実行できませんでした。
|