LoginSignup
6
7

More than 5 years have passed since last update.

DownloadManagerの妙な挙動

Posted at

DownloadManagerでダウンロード完了したはずなのにファイルが無くなっている現象が起きていたので調べてみました。

D/DownloadResultReceiver( 1585): onReceive: intent=Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=com.example.downloadtest cmp=com.example.downloadtest/com.example.downloadtest.receiver.DownloadResultReceiver (has extras) }
D/DownloadResultIntentService( 1585): handleDownloadComplete: id=803
D/DownloadResultIntentService( 1585): status=8, reason=0, bytes=12805521, localUri=file:///storage/sdcard0/Android/data/com.example.downloadtest/files/test.mp4

D/DownloadResultReceiver( 1585): onReceive: intent=Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=com.example.downloadtest cmp=com.example.downloadtest/com.example.downloadtest.receiver.DownloadResultReceiver (has extras) }
D/DownloadResultIntentService( 1585): handleDownloadComplete: id=803
D/DownloadResultIntentService( 1585): status=16, reason=1008, bytes=12805521, localUri=null

同じリクエストID(id=803)でDOWNLOAD_COMPLETEのブロードキャストインテントが2度来ていて、最初はstatus=8(STATUS_SUCCESSFUL)だけどその後のはstatus=16(STATUS_FAILED)になっていてダウンロードしたファイルも削除されています。

余談ですがSTATUS_SUCCESSFULが2回来るケースもありました。アプリ内でダウンロード完了時に処理をする場合には注意した方がいいでしょう(´・ω・`)

いろいろぐぐって見たところAndroidのバグっぽい
https://code.google.com/p/android/issues/detail?id=18462
https://code.google.com/p/android/issues/detail?id=34791

どうもサーバー側でETagを設定しないと消される場合があるみたい
http://papaya-backend.net/2013/04/12/why-http-etag-header-may-cause-your-downloading-apps-on-android-failed/

ただ、今回はダウンロード元が外部のサーバーでそうするわけにも行かないので

  1. 一時ファイルをDownloadManagerに渡してダウンロード
  2. DOWNLOAD_COMPLETEでダウンロード完了した際にファイル名をrenameしてDownloadManagerが削除できないようにする

という形で逃げましたがrenameしたのにDownloadManagerが一時ファイルをfilesize=0の状態で作ってしまうので定期的に空ファイルのお掃除は必要かもしれません

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7