List of usage examples for android.app DownloadManager getUriForDownloadedFile
public Uri getUriForDownloadedFile(long id)
From source file:Main.java
/** * Decode downloaded big picture.//w ww. j ava 2 s.c o m * @param context any application context. * @param downloadId downloaded picture identifier. * @return decoded bitmap if successful, null on error. */ public static Bitmap getBigPicture(Context context, long downloadId) { /* Decode bitmap */ InputStream stream = null; try { /* * Query download manager to get file. FIXME For an unknown reason, using the file descriptor * fails after APK build with ProGuard, we use stream instead. */ DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); Uri uri = downloadManager.getUriForDownloadedFile(downloadId); ContentResolver contentResolver = context.getContentResolver(); stream = contentResolver.openInputStream(uri); /* * Bitmaps larger than 2048 in any dimension are likely to cause OutOfMemoryError in the * NotificationManager or even here. Plus some devices simply don't accept such images: the * notification manager can drop the notification without any way to check that via API. To * avoid the problem, sub sample the image in an efficient way (not using Bitmap matrix * scaling after decoding the bitmap with original size: it could run out of memory when * decoding the full image). FIXME There is center cropping applied by the NotificationManager * on the bitmap we provide, we can't avoid it, see * https://code.google.com/p/android/issues/detail?id=58318. */ BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; options.inSampleSize = 1; options.inPreferQualityOverSpeed = true; /* Decode dimensions */ BitmapFactory.decodeStream(stream, null, options); int maxDim = Math.max(options.outWidth, options.outHeight); /* Compute sub sample size (it must be a power of 2) */ while (maxDim > 2048) { options.inSampleSize <<= 1; maxDim >>= 1; } /* Decode actual bitmap */ options.inJustDecodeBounds = false; stream.close(); stream = contentResolver.openInputStream(uri); return BitmapFactory.decodeStream(stream, null, options); } catch (Throwable t) { /* Abort, causes are likely FileNotFoundException or OutOfMemoryError */ return null; } finally { /* Silently close stream */ if (stream != null) try { stream.close(); } catch (IOException e) { } } }
From source file:com.android.emailcommon.utility.AttachmentUtilities.java
/** * Save the attachment to its final resting place (cache or sd card) *//*from w w w . j ava 2 s. c o m*/ public static void saveAttachment(Context context, InputStream in, Attachment attachment) { Uri uri = ContentUris.withAppendedId(Attachment.CONTENT_URI, attachment.mId); ContentValues cv = new ContentValues(); long attachmentId = attachment.mId; long accountId = attachment.mAccountKey; String contentUri; long size; try { if (attachment.mUiDestination == UIProvider.AttachmentDestination.CACHE) { File saveIn = getAttachmentDirectory(context, accountId); if (!saveIn.exists()) { saveIn.mkdirs(); } File file = getAttachmentFilename(context, accountId, attachmentId); file.createNewFile(); size = copyFile(in, file); contentUri = getAttachmentUri(accountId, attachmentId).toString(); } else if (Utility.isExternalStorageMounted()) { File downloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); downloads.mkdirs(); File file = Utility.createUniqueFile(downloads, attachment.mFileName); size = copyFile(in, file); String absolutePath = file.getAbsolutePath(); // Although the download manager can scan media files, scanning only happens // after the user clicks on the item in the Downloads app. So, we run the // attachment through the media scanner ourselves so it gets added to // gallery / music immediately. MediaScannerConnection.scanFile(context, new String[] { absolutePath }, null, null); DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); long id = dm.addCompletedDownload(attachment.mFileName, attachment.mFileName, false /* do not use media scanner */, attachment.mMimeType, absolutePath, size, true /* show notification */); contentUri = dm.getUriForDownloadedFile(id).toString(); } else { Log.w(Logging.LOG_TAG, "Trying to save an attachment without external storage?"); throw new IOException(); } // Update the attachment cv.put(AttachmentColumns.SIZE, size); cv.put(AttachmentColumns.CONTENT_URI, contentUri); cv.put(AttachmentColumns.UI_STATE, UIProvider.AttachmentState.SAVED); } catch (IOException e) { // Handle failures here... cv.put(AttachmentColumns.UI_STATE, UIProvider.AttachmentState.FAILED); } context.getContentResolver().update(uri, cv, null, null); }
From source file:com.chen.emailcommon.utility.AttachmentUtilities.java
/** * Save the attachment to its final resting place (cache or sd card) *//*from w w w .ja va 2s. co m*/ public static void saveAttachment(Context context, InputStream in, Attachment attachment) { Uri uri = ContentUris.withAppendedId(Attachment.CONTENT_URI, attachment.mId); ContentValues cv = new ContentValues(); long attachmentId = attachment.mId; long accountId = attachment.mAccountKey; String contentUri = null; long size; try { ContentResolver resolver = context.getContentResolver(); if (attachment.mUiDestination == UIProvider.AttachmentDestination.CACHE) { Uri attUri = getAttachmentUri(accountId, attachmentId); size = copyFile(in, resolver.openOutputStream(attUri)); contentUri = attUri.toString(); } else if (Utility.isExternalStorageMounted()) { File downloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); downloads.mkdirs(); File file = Utility.createUniqueFile(downloads, attachment.mFileName); size = copyFile(in, new FileOutputStream(file)); String absolutePath = file.getAbsolutePath(); // Although the download manager can scan media files, scanning only happens // after the user clicks on the item in the Downloads app. So, we run the // attachment through the media scanner ourselves so it gets added to // gallery / music immediately. MediaScannerConnection.scanFile(context, new String[] { absolutePath }, null, null); DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); long id = dm.addCompletedDownload(attachment.mFileName, attachment.mFileName, false /* do not use media scanner */, attachment.mMimeType, absolutePath, size, true /* show notification */); contentUri = dm.getUriForDownloadedFile(id).toString(); } else { LogUtils.w(Logging.LOG_TAG, "Trying to save an attachment without external storage?"); throw new IOException(); } // Update the attachment cv.put(AttachmentColumns.SIZE, size); cv.put(AttachmentColumns.CONTENT_URI, contentUri); cv.put(AttachmentColumns.UI_STATE, UIProvider.AttachmentState.SAVED); } catch (IOException e) { // Handle failures here... cv.put(AttachmentColumns.UI_STATE, UIProvider.AttachmentState.FAILED); } context.getContentResolver().update(uri, cv, null, null); // If this is an inline attachment, update the body if (contentUri != null && attachment.mContentId != null) { Body body = Body.restoreBodyWithMessageId(context, attachment.mMessageKey); if (body != null && body.mHtmlContent != null) { cv.clear(); String html = body.mHtmlContent; String contentIdRe = "\\s+(?i)src=\"cid(?-i):\\Q" + attachment.mContentId + "\\E\""; String srcContentUri = " src=\"" + contentUri + "\""; html = html.replaceAll(contentIdRe, srcContentUri); cv.put(BodyColumns.HTML_CONTENT, html); context.getContentResolver().update(ContentUris.withAppendedId(Body.CONTENT_URI, body.mId), cv, null, null); } } }
From source file:com.indeema.emailcommon.utility.AttachmentUtilities.java
/** * Save the attachment to its final resting place (cache or sd card) *///from ww w . j a v a2s. c o m public static void saveAttachment(Context context, InputStream in, Attachment attachment) { Uri uri = ContentUris.withAppendedId(Attachment.CONTENT_URI, attachment.mId); ContentValues cv = new ContentValues(); long attachmentId = attachment.mId; long accountId = attachment.mAccountKey; String contentUri = null; long size; try { ContentResolver resolver = context.getContentResolver(); if (attachment.mUiDestination == UIProvider.AttachmentDestination.CACHE) { Uri attUri = getAttachmentUri(accountId, attachmentId); size = copyFile(in, resolver.openOutputStream(attUri)); contentUri = attUri.toString(); } else if (Utility.isExternalStorageMounted()) { if (attachment.mFileName == null) { // TODO: This will prevent a crash but does not surface the underlying problem // to the user correctly. LogUtils.w(Logging.LOG_TAG, "Trying to save an attachment with no name: %d", attachmentId); throw new IOException("Can't save an attachment with no name"); } File downloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); downloads.mkdirs(); File file = Utility.createUniqueFile(downloads, attachment.mFileName); size = copyFile(in, new FileOutputStream(file)); String absolutePath = file.getAbsolutePath(); // Although the download manager can scan media files, scanning only happens // after the user clicks on the item in the Downloads app. So, we run the // attachment through the media scanner ourselves so it gets added to // gallery / music immediately. MediaScannerConnection.scanFile(context, new String[] { absolutePath }, null, null); DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); long id = dm.addCompletedDownload(attachment.mFileName, attachment.mFileName, false /* do not use media scanner */, attachment.mMimeType, absolutePath, size, true /* show notification */); contentUri = dm.getUriForDownloadedFile(id).toString(); } else { LogUtils.w(Logging.LOG_TAG, "Trying to save an attachment without external storage?"); throw new IOException(); } // Update the attachment cv.put(AttachmentColumns.SIZE, size); cv.put(AttachmentColumns.CONTENT_URI, contentUri); cv.put(AttachmentColumns.UI_STATE, UIProvider.AttachmentState.SAVED); } catch (IOException e) { // Handle failures here... cv.put(AttachmentColumns.UI_STATE, UIProvider.AttachmentState.FAILED); } context.getContentResolver().update(uri, cv, null, null); // If this is an inline attachment, update the body if (contentUri != null && attachment.mContentId != null) { Body body = Body.restoreBodyWithMessageId(context, attachment.mMessageKey); if (body != null && body.mHtmlContent != null) { cv.clear(); String html = body.mHtmlContent; String contentIdRe = "\\s+(?i)src=\"cid(?-i):\\Q" + attachment.mContentId + "\\E\""; String srcContentUri = " src=\"" + contentUri + "\""; html = html.replaceAll(contentIdRe, srcContentUri); cv.put(BodyColumns.HTML_CONTENT, html); context.getContentResolver().update(ContentUris.withAppendedId(Body.CONTENT_URI, body.mId), cv, null, null); } } }
From source file:com.tct.emailcommon.utility.AttachmentUtilities.java
/** * Save the attachment to its final resting place (cache or sd card) *///w w w. jav a 2 s. c o m public static long saveAttachment(Context context, InputStream in, Attachment attachment) { final Uri uri = ContentUris.withAppendedId(Attachment.CONTENT_URI, attachment.mId); final ContentValues cv = new ContentValues(); final long attachmentId = attachment.mId; final long accountId = attachment.mAccountKey; //TS: wenggangjin 2014-12-11 EMAIL BUGFIX_868520 MOD_S String contentUri = null; //TS: wenggangjin 2014-12-11 EMAIL BUGFIX_868520 MOD_E String realUri = null; //TS: zheng.zou 2016-1-22 EMAIL BUGFIX-1431088 ADD long size = 0; try { ContentResolver resolver = context.getContentResolver(); if (attachment.mUiDestination == UIProvider.UIPROVIDER_ATTACHMENTDESTINATION_CACHE) { LogUtils.i(LogUtils.TAG, "AttachmentUtilities saveAttachment when attachment destination is cache", "attachment.size:" + attachment.mSize); Uri attUri = getAttachmentUri(accountId, attachmentId); size = copyFile(in, resolver.openOutputStream(attUri)); contentUri = attUri.toString(); } else if (Utility.isExternalStorageMounted()) { LogUtils.i(LogUtils.TAG, "AttachmentUtilities saveAttachment to storage", "attachment.size:" + attachment.mSize); if (TextUtils.isEmpty(attachment.mFileName)) { // TODO: This will prevent a crash but does not surface the underlying problem // to the user correctly. LogUtils.w(Logging.LOG_TAG, "Trying to save an attachment with no name: %d", attachmentId); throw new IOException("Can't save an attachment with no name"); } //TS: zheng.zou 2016-1-22 EMAIL BUGFIX-1431088 ADD_S String exchange = "com.tct.exchange"; if (exchange.equals(context.getPackageName()) && !PermissionUtil .checkPermissionAndLaunchExplain(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { throw new IOException("Can't save an attachment due to no Storage permission"); } //TS: zheng.zou 2016-1-22 EMAIL BUGFIX-1431088 ADD_E File downloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); downloads.mkdirs(); File file = Utility.createUniqueFile(downloads, attachment.mFileName); size = copyFile(in, new FileOutputStream(file)); String absolutePath = file.getAbsolutePath(); realUri = "file://" + absolutePath; //TS: zheng.zou 2016-1-22 EMAIL BUGFIX-1431088 ADD // Although the download manager can scan media files, scanning only happens // after the user clicks on the item in the Downloads app. So, we run the // attachment through the media scanner ourselves so it gets added to // gallery / music immediately. MediaScannerConnection.scanFile(context, new String[] { absolutePath }, null, null); final String mimeType = TextUtils.isEmpty(attachment.mMimeType) ? "application/octet-stream" : attachment.mMimeType; try { DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); //TS: junwei-xu 2016-02-04 EMAIL BUGFIX-1531245 MOD_S //Note: should use media scanner, it will allow update the //media provider uri column in download manager's database. long id = dm.addCompletedDownload(attachment.mFileName, attachment.mFileName, true /* use media scanner */, mimeType, absolutePath, size, true /* show notification */); //TS: junwei-xu 2016-02-04 EMAIL BUGFIX-1531245 MOD_E contentUri = dm.getUriForDownloadedFile(id).toString(); } catch (final IllegalArgumentException e) { LogUtils.d(LogUtils.TAG, e, "IAE from DownloadManager while saving attachment"); throw new IOException(e); } } else { LogUtils.w(Logging.LOG_TAG, "Trying to save an attachment without external storage?"); throw new IOException(); } // Update the attachment cv.put(AttachmentColumns.SIZE, size); cv.put(AttachmentColumns.CONTENT_URI, contentUri); cv.put(AttachmentColumns.UI_STATE, UIProvider.UIPROVIDER_ATTACHMENTSTATE_SAVED); //TS: zheng.zou 2016-1-22 EMAIL BUGFIX-1431088 ADD_S if (realUri != null) { cv.put(AttachmentColumns.REAL_URI, realUri); } //TS: zheng.zou 2016-1-22 EMAIL BUGFIX-1431088 ADD_E } catch (IOException e) { LogUtils.e(Logging.LOG_TAG, e, "Fail to save attachment to storage!"); // Handle failures here... cv.put(AttachmentColumns.UI_STATE, UIProvider.UIPROVIDER_ATTACHMENTSTATE_FAILED); } context.getContentResolver().update(uri, cv, null, null); //TS: wenggangjin 2014-12-11 EMAIL BUGFIX_868520 MOD_S // If this is an inline attachment, update the body if (contentUri != null && attachment.mContentId != null && attachment.mContentId.length() > 0) { Body body = Body.restoreBodyWithMessageId(context, attachment.mMessageKey); if (body != null && body.mHtmlContent != null) { cv.clear(); String html = body.mHtmlContent; String contentIdRe = "\\s+(?i)src=\"cid(?-i):\\Q" + attachment.mContentId + "\\E\""; //TS: zhaotianyong 2015-03-23 EXCHANGE BUGFIX_899799 MOD_S //TS: zhaotianyong 2015-04-01 EXCHANGE BUGFIX_962560 MOD_S String srcContentUri = " src=\"" + contentUri + "\""; //TS: zhaotianyong 2015-04-01 EXCHANGE BUGFIX_962560 MOD_E //TS: zhaotianyong 2015-03-23 EXCHANGE BUGFIX_899799 MOD_E //TS: zhaotianyong 2015-04-15 EMAIL BUGFIX_976967 MOD_S try { html = html.replaceAll(contentIdRe, srcContentUri); } catch (PatternSyntaxException e) { LogUtils.w(Logging.LOG_TAG, "Unrecognized backslash escape sequence in pattern"); } //TS: zhaotianyong 2015-04-15 EMAIL BUGFIX_976967 MOD_E cv.put(BodyColumns.HTML_CONTENT, html); Body.updateBodyWithMessageId(context, attachment.mMessageKey, cv); Body.restoreBodyHtmlWithMessageId(context, attachment.mMessageKey); } } //TS: wenggangjin 2014-12-11 EMAIL BUGFIX_868520 MOD_E return size; }
From source file:com.tct.emailcommon.utility.AttachmentUtilities.java
public static void saveAttachmentToExternal(Context context, Attachment attachment, String path) { final Uri uri = ContentUris.withAppendedId(Attachment.CONTENT_URI, attachment.mId); final ContentValues cv = new ContentValues(); final long attachmentId = attachment.mId; final long accountId = attachment.mAccountKey; //TS: wenggangjin 2014-12-11 EMAIL BUGFIX_868520 MOD_S String contentUri = null;//from w ww . ja v a 2 s .c om //TS: wenggangjin 2014-12-11 EMAIL BUGFIX_868520 MOD_S final long size; InputStream in = null; OutputStream out = null; try { ContentResolver resolver = context.getContentResolver(); if (Utility.isExternalStorageMounted()) { if (TextUtils.isEmpty(attachment.mFileName)) { // TODO: This will prevent a crash but does not surface the underlying problem // to the user correctly. LogUtils.w(Logging.LOG_TAG, "Trying to save an attachment with no name: %d", attachmentId); throw new IOException("Can't save an attachment with no name"); } // TS: Gantao 2015-07-29 EMAIL BUGFIX-1055568 MOD_S try { String cachedFileUri = attachment.getCachedFileUri(); if (TextUtils.isEmpty(cachedFileUri)) { throw new IOException(); } in = resolver.openInputStream(Uri.parse(cachedFileUri)); } catch (IOException e) { String contentUriForOpen = attachment.getContentUri(); if (TextUtils.isEmpty(contentUriForOpen)) { throw new IOException(); } in = resolver.openInputStream(Uri.parse(contentUriForOpen)); //TS: junwei-xu 2016-03-31 EMAIL BUGFIX-1886442 ADD_S } catch (IllegalArgumentException e) { String contentUriForOpen = attachment.getContentUri(); if (TextUtils.isEmpty(contentUriForOpen)) { throw new IOException(); } in = resolver.openInputStream(Uri.parse(contentUriForOpen)); } //TS: junwei-xu 2016-03-31 EMAIL BUGFIX-1886442 ADD_E //TS: jian.xu 2016-01-20 EMAIL FEATURE-1477377 MOD_S //Note: we support save attachment at user designated location. File downloads; if (path != null) { downloads = new File(path); } else { downloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); } //TS: jian.xu 2016-01-20 EMAIL FEATURE-1477377 MOD_E downloads.mkdirs(); File file = Utility.createUniqueFile(downloads, attachment.mFileName); out = new FileOutputStream(file); size = copyFile(in, out); String absolutePath = file.getAbsolutePath(); // Although the download manager can scan media files, scanning only happens // after the user clicks on the item in the Downloads app. So, we run the // attachment through the media scanner ourselves so it gets added to // gallery / music immediately. MediaScannerConnection.scanFile(context, new String[] { absolutePath }, null, null); final String mimeType = TextUtils.isEmpty(attachment.mMimeType) ? "application/octet-stream" : attachment.mMimeType; try { DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); //TS: junwei-xu 2016-02-04 EMAIL BUGFIX-1531245 MOD_S //Note: should use media scanner, it will allow update the //media provider uri column in download manager's database. long id = dm.addCompletedDownload(attachment.mFileName, attachment.mFileName, true /* use media scanner */, mimeType, absolutePath, size, true /* show notification */); //TS: junwei-xu 2016-02-04 EMAIL BUGFIX-1531245 MOD_E contentUri = dm.getUriForDownloadedFile(id).toString(); } catch (final IllegalArgumentException e) { LogUtils.d(LogUtils.TAG, e, "IAE from DownloadManager while saving attachment"); throw new IOException(e); } } else { LogUtils.w(Logging.LOG_TAG, "Trying to save an attachment without external storage?"); throw new IOException(); } // Update the attachment cv.put(AttachmentColumns.SIZE, size); cv.put(AttachmentColumns.UI_STATE, UIProvider.UIPROVIDER_ATTACHMENTSTATE_SAVED); // TS: Gantao 2015-06-30 EMAIL BUGFIX-1031608 ADD_S //Note:we have saved the attachment to sd card,so should update the attachment destination external cv.put(AttachmentColumns.UI_DESTINATION, UIProvider.UIPROVIDER_ATTACHMENTDESTINATION_EXTERNAL); // TS: Gantao 2015-06-30 EMAIL BUGFIX-1031608 ADD_E } catch (IOException e) { // Handle failures here... LogUtils.e(Logging.LOG_TAG, "IOException while save an attachment to external storage"); } finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } } catch (IOException e) { LogUtils.e(Logging.LOG_TAG, "ioexception while close the stream"); } } // TS: Gantao 2015-07-29 EMAIL BUGFIX-1055568 MOD_E //TS: wenggangjin 2014-12-10 EMAIL BUGFIX_871936 MOD_S // context.getContentResolver().update(uri, cv, null, null); if (cv.size() > 0) { context.getContentResolver().update(uri, cv, null, null); } //TS: wenggangjin 2014-12-10 EMAIL BUGFIX_871936 MOD_E //TS: wenggangjin 2014-12-11 EMAIL BUGFIX_868520 MOD_S if (contentUri != null && attachment.mContentId != null && attachment.mContentId.length() > 0) { Body body = Body.restoreBodyWithMessageId(context, attachment.mMessageKey); if (body != null && body.mHtmlContent != null) { cv.clear(); String html = body.mHtmlContent; String contentIdRe = "\\s+(?i)src=\"cid(?-i):\\Q" + attachment.mContentId + "\\E\""; String srcContentUri = " src=\"" + contentUri + "\""; //TS: zhaotianyong 2015-04-15 EMAIL BUGFIX_976967 MOD_S try { html = html.replaceAll(contentIdRe, srcContentUri); } catch (PatternSyntaxException e) { LogUtils.w(Logging.LOG_TAG, "Unrecognized backslash escape sequence in pattern"); } //TS: zhaotianyong 2015-04-15 EMAIL BUGFIX_976967 MOD_E cv.put(BodyColumns.HTML_CONTENT, html); Body.updateBodyWithMessageId(context, attachment.mMessageKey, cv); Body.restoreBodyHtmlWithMessageId(context, attachment.mMessageKey); } } //TS: wenggangjin 2014-12-11 EMAIL BUGFIX_868520 MOD_E }
From source file:com.oakesville.mythling.app.AppData.java
public Map<String, Download> getDownloads() throws IOException, JSONException, ParseException { readDownloads();//from w w w.jav a 2 s .c o m DownloadManager dm = (DownloadManager) appContext.getSystemService(Context.DOWNLOAD_SERVICE); Map<String, Download> filtered = new HashMap<String, Download>(); Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -7); long lastWeek = cal.getTimeInMillis(); List<String> itemsToRemove = null; for (String itemId : downloads.keySet()) { Download download = downloads.get(itemId); if (dm.getUriForDownloadedFile(download.getDownloadId()) != null) // make sure the file exists filtered.put(itemId, download); else if (lastWeek > download.getStarted().getTime()) { // remove missing items older than a week if (itemsToRemove == null) itemsToRemove = new ArrayList<String>(); itemsToRemove.add(download.getItemId()); } } if (itemsToRemove != null) { for (String itemToRemove : itemsToRemove) downloads.remove(itemToRemove); persistDownloads(); } return filtered; }
From source file:com.oakesville.mythling.MediaActivity.java
protected Uri getDownload(Item item) throws IOException { if (item.getDownloadId() == null) return null; DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); Uri uri = dm.getUriForDownloadedFile(item.getDownloadId()); if (uri == null) { item.setDownloadId(null);/*from w ww.j a va2s . co m*/ throw new IOException("Cannot find download for: " + item); } else { return uri; } }
From source file:org.chromium.chrome.browser.download.DownloadManagerService.java
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (!DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) return;//w w w . jav a 2 s. c om final DownloadManager manager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE); long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); if (downloadId == -1) return; boolean isPendingOMADownload = mOMADownloadHandler.isPendingOMADownload(downloadId); boolean isInOMASharedPrefs = isDownloadIdInOMASharedPrefs(downloadId); if (isPendingOMADownload || isInOMASharedPrefs) { clearPendingOMADownload(downloadId, null); mPendingAutoOpenDownloads.remove(downloadId); } else if (mPendingAutoOpenDownloads.get(downloadId) != null) { Cursor c = manager.query(new DownloadManager.Query().setFilterById(downloadId)); int statusIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS); while (c.moveToNext()) { int status = c.getInt(statusIndex); DownloadInfo info = mPendingAutoOpenDownloads.get(downloadId); switch (status) { case DownloadManager.STATUS_SUCCESSFUL: try { mPendingAutoOpenDownloads.remove(downloadId); if (OMADownloadHandler.OMA_DOWNLOAD_DESCRIPTOR_MIME.equalsIgnoreCase(info.getMimeType())) { mOMADownloadHandler.handleOMADownload(info, downloadId); manager.remove(downloadId); break; } Uri uri = manager.getUriForDownloadedFile(downloadId); Intent launchIntent = new Intent(Intent.ACTION_VIEW); launchIntent.setDataAndType(uri, manager.getMimeTypeForDownloadedFile(downloadId)); launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mContext.startActivity(launchIntent); } catch (ActivityNotFoundException e) { Log.w(TAG, "Activity not found."); } break; case DownloadManager.STATUS_FAILED: mPendingAutoOpenDownloads.remove(downloadId); break; default: break; } } } if (mPendingAutoOpenDownloads.size() == 0) { mContext.unregisterReceiver(this); } }