Java Code Examples for android.app.DownloadManager#addCompletedDownload()

The following examples show how to use android.app.DownloadManager#addCompletedDownload() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommonActivityUtils.java    From matrix-android-console with Apache License 2.0 6 votes vote down vote up
/**
 * Save a media URI into the download directory
 * @param context the context
 * @param srcFile the source file.
 * @param filename the filename (optional)
 * @return the downloads file path
 */
@SuppressLint("NewApi")
public static String saveMediaIntoDownloads(Context context, File srcFile, String filename, String mimeType) {
    String fullFilePath = saveFileInto(context, srcFile, Environment.DIRECTORY_DOWNLOADS, filename);

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        if (null != fullFilePath) {
            DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);

            try {
                File file = new File(fullFilePath);
                downloadManager.addCompletedDownload(file.getName(), file.getName(), true, mimeType, file.getAbsolutePath(), file.length(), true);
            } catch (Exception e) {
            }
        }
    }

    return fullFilePath;
}
 
Example 2
Source File: TaskDetailActivity.java    From Kandroid with GNU General Public License v3.0 5 votes vote down vote up
@Override
        public void onDownloadTaskFile(boolean success, int id, String data) {
            if (success) {
                byte[] inData = Base64.decode(data, Base64.DEFAULT);
                for (KanboardTaskFile f: files) {
                    if (f.getId() == id) {
                        try {
                            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), f.getName());
                            FileOutputStream outData = new FileOutputStream(file);
                            outData.write(inData);
                            outData.close();
                            String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString()));
                            if (mime == null) {
                                mime = "application/octet-stream";
                            }
                            if (BuildConfig.DEBUG) {
                                Log.d(Constants.TAG, Uri.fromFile(file).toString());
                                Log.d(Constants.TAG, mime);
                            }
                            DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                            dm.addCompletedDownload(file.getName(), "Kandroid download", false, mime, file.getPath(), file.length(), true);
//                            Snackbar.make(findViewById(R.id.root_layout), String.format(Locale.getDefault(), "Saved file to: %s", file.getPath()), Snackbar.LENGTH_LONG).show();
                        } catch (IOException e) {
                            Log.w(Constants.TAG, "IOError writing file");
                            e.printStackTrace();
                        }
                        break;
                    }
                }
            } else {
                Snackbar.make(findViewById(R.id.root_layout), "Unable to download file", Snackbar.LENGTH_LONG).show();
            }
        }
 
Example 3
Source File: OMADownloadHandler.java    From delion with Apache License 2.0 5 votes vote down vote up
@Override
protected void onPostExecute(Boolean success) {
    DownloadManager manager =
            (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
    if (success) {
        String path = mDownloadInfo.getFilePath();
        if (!TextUtils.isEmpty(path)) {
            // Move the downloaded content from the app directory to public directory.
            File fromFile = new File(path);
            String fileName = fromFile.getName();
            File toFile = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DOWNLOADS), fileName);
            if (fromFile.renameTo(toFile)) {
                manager.addCompletedDownload(
                        fileName, mDownloadInfo.getDescription(), false,
                        mDownloadInfo.getMimeType(), toFile.getPath(),
                        mDownloadInfo.getContentLength(), true);
            } else if (fromFile.delete()) {
                Log.w(TAG, "Failed to rename the file.");
                return;
            } else {
                Log.w(TAG, "Failed to rename and delete the file.");
            }
        }
        showNextUrlDialog(mOMAInfo);
    } else if (mDownloadId != DownloadItem.INVALID_DOWNLOAD_ID) {
        // Remove the downloaded content.
        manager.remove(mDownloadId);
    }
}
 
Example 4
Source File: DownloadManagerDelegate.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
 * @see android.app.DownloadManager#addCompletedDownload(String, String, boolean, String,
 * String, long, boolean)
 */
protected long addCompletedDownload(String fileName, String description, String mimeType,
        String path, long length, String originalUrl, String referer) {
    DownloadManager manager =
            (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
    return manager.addCompletedDownload(fileName, description, true, mimeType, path, length,
            false);
}
 
Example 5
Source File: TaskDetailActivity.java    From Kandroid with GNU General Public License v3.0 5 votes vote down vote up
@Override
        public void onDownloadTaskFile(boolean success, int id, String data) {
            if (success) {
                byte[] inData = Base64.decode(data, Base64.DEFAULT);
                for (KanboardTaskFile f: files) {
                    if (f.getId() == id) {
                        try {
                            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), f.getName());
                            FileOutputStream outData = new FileOutputStream(file);
                            outData.write(inData);
                            outData.close();
                            String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString()));
                            if (mime == null) {
                                mime = "application/octet-stream";
                            }
                            if (BuildConfig.DEBUG) {
                                Log.d(Constants.TAG, Uri.fromFile(file).toString());
                                Log.d(Constants.TAG, mime);
                            }
                            DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                            dm.addCompletedDownload(file.getName(), "Kandroid download", false, mime, file.getPath(), file.length(), true);
//                            Snackbar.make(findViewById(R.id.root_layout), String.format(Locale.getDefault(), "Saved file to: %s", file.getPath()), Snackbar.LENGTH_LONG).show();
                        } catch (IOException e) {
                            Log.w(Constants.TAG, "IOError writing file");
                            e.printStackTrace();
                        }
                        break;
                    }
                }
            } else {
                Snackbar.make(findViewById(R.id.root_layout), "Unable to download file", Snackbar.LENGTH_LONG).show();
            }
        }
 
Example 6
Source File: OMADownloadHandler.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
@Override
protected void onPostExecute(Boolean success) {
    DownloadManager manager =
            (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
    if (success) {
        String path = mDownloadInfo.getFilePath();
        if (!TextUtils.isEmpty(path)) {
            // Move the downloaded content from the app directory to public directory.
            File fromFile = new File(path);
            String fileName = fromFile.getName();
            File toFile = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DOWNLOADS), fileName);
            if (fromFile.renameTo(toFile)) {
                manager.addCompletedDownload(
                        fileName, mDownloadInfo.getDescription(), false,
                        mDownloadInfo.getMimeType(), toFile.getPath(),
                        mDownloadInfo.getContentLength(), true);
            } else if (fromFile.delete()) {
                Log.w(TAG, "Failed to rename the file.");
                return;
            } else {
                Log.w(TAG, "Failed to rename and delete the file.");
            }
        }
        showNextUrlDialog(mOMAInfo);
    } else if (mDownloadId != DownloadItem.INVALID_DOWNLOAD_ID) {
        // Remove the downloaded content.
        manager.remove(mDownloadId);
    }
}
 
Example 7
Source File: OMADownloadHandler.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
protected void onPostExecute(Boolean success) {
    DownloadManager manager =
            (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
    if (success) {
        String path = mDownloadInfo.getFilePath();
        if (!TextUtils.isEmpty(path)) {
            // Move the downloaded content from the app directory to public directory.
            File fromFile = new File(path);
            String fileName = fromFile.getName();
            File toFile = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DOWNLOADS), fileName);
            if (fromFile.renameTo(toFile)) {
                manager.addCompletedDownload(
                        fileName, mDownloadInfo.getDescription(), false,
                        mDownloadInfo.getMimeType(), toFile.getPath(),
                        mDownloadInfo.getBytesReceived(), true);
            } else if (fromFile.delete()) {
                Log.w(TAG, "Failed to rename the file.");
                return;
            } else {
                Log.w(TAG, "Failed to rename and delete the file.");
            }
        }
        showNextUrlDialog(mOMAInfo);
    } else if (mDownloadId != DownloadItem.INVALID_DOWNLOAD_ID) {
        // Remove the downloaded content.
        manager.remove(mDownloadId);
    }
}
 
Example 8
Source File: ExportService.java    From science-journal with Apache License 2.0 4 votes vote down vote up
@TargetApi(VERSION_CODES.O)
private static void copyToDownload(
    Context context, Uri sourceUri, String fileName, File destination) {
  try {
    if (AndroidVersionUtils.isApiLevelAtLeastOreo()) {
      Files.copy(
          context.getContentResolver().openInputStream(sourceUri),
          destination.toPath(),
          StandardCopyOption.REPLACE_EXISTING);
    } else {
      UpdateExperimentFragment.copyUriToFile(context, sourceUri, destination);
      // Inform DownloadManager of completed download so the file shows in Downloads app
      DownloadManager downloadManager =
          (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
      downloadManager.addCompletedDownload(
          destination.getName(),
          destination.getName(),
          true,
          getMimeTypeFromFileName(fileName),
          destination.getAbsolutePath(),
          destination.length(),
          false);
    }
    Intent intent = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
    PendingIntent pendingIntent =
        PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_ONE_SHOT);
    ((NotificationManager)
            context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE))
        .notify(
            NotificationIds.SAVED_TO_DEVICE,
            new NotificationCompat.Builder(
                    context.getApplicationContext(), NotificationChannels.SAVE_TO_DEVICE_CHANNEL)
                .setContentTitle(fileName)
                .setContentText(context.getString(R.string.saved_to_device_text))
                .setSubText(context.getString(R.string.save_to_device_channel_description))
                .setSmallIcon(R.drawable.ic_notification_24dp)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .build());
  } catch (IOException ioe) {
    AppSingleton.getInstance(context)
        .onNextActivity()
        .subscribe(
            activity -> {
              AccessibilityUtils.makeSnackbar(
                      activity.findViewById(android.R.id.content),
                      context.getResources().getString(R.string.saved_to_device_error),
                      Snackbar.LENGTH_SHORT)
                  .show();
            });
    ioe.printStackTrace();
    if (destination.exists()) {
      destination.delete();
    }
  } finally {
    AppSingleton.getInstance(context).setExportServiceBusy(false);
  }
}