Я столкнулся с проблемой открытия загруженного файла после успешной загрузки через API DownloadManager. В моем коде:
Uri uri=Uri.parse("http://www.nasa.gov/images/content/206402main_jsc2007e113280_hires.jpg");
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
.mkdirs();
lastDownload = mgr.enqueue(new DownloadManager.Request(uri)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("app update")
.setDescription("New version 1.1")
.setShowRunningNotification(true)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "a.apk"));
Cursor c=mgr.query(new DownloadManager.Query().setFilterById(lastDownload));
if(c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)) == 8) {
try {
mgr.openDownloadedFile(c.getLong(c.getColumnIndex(DownloadManager.COLUMN_ID)));
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("MGR", "Error");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("MGR", "Error");
}
}
Проблема в том, когда вызывается if(c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))==8)
. Я получил статус -1 и исключение. Есть ли лучший способ, как открыть загруженные файлы с DownloadManager API
? В моем примере я загружаю большое изображение, в реальной ситуации я загружаю файл APK
, и мне нужно отобразить диалоговое окно установки сразу после обновления.
Изменить: Я понял, что статус = 8 после успешной загрузки. У вас может быть другой подход "проверка успешной загрузки"
Спасибо