Android save file to download folder
Add complete download #
Before android 10, downloadManger provide a function called addCompletedDownload to download a local file. This function will add the given file to download folder and send a notification just like it download from the internet. However, this function is deprecated in API 29. Now we can only use MediaStore to manually copy the file to download folder and send a notification from our app.
/**
* Copy a local file to download folder and send a download complete notification
* @param file file to copy
* @param context context
*/
public static void downloadFile(File file, Context context) {
String mimeType = getMimeType(Uri.fromFile(file), context);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
// copy file to download folder
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Downloads.TITLE, file.getName());
contentValues.put(MediaStore.Downloads.DISPLAY_NAME, file.getName());
contentValues.put(MediaStore.Downloads.MIME_TYPE, mimeType);
contentValues.put(MediaStore.Downloads.SIZE, file.length());
Uri uri = context.getContentResolver().insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues);
try {
FileInputStream inputStream = new FileInputStream(file);
FileOutputStream outputStream = (FileOutputStream) context.getContentResolver().openOutputStream(uri);
FileChannel inChannel = inputStream.getChannel();
FileChannel outChannel = outputStream.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
inputStream.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
// send download complete notification
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, mimeType);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, DOWNLOAD_NOTIFICATION_CHANNEL)
.setContentTitle(file.getName() + " is downloaded")
.setSmallIcon(R.mipmap.ic_launcher_round)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(Instant.now().getNano(), builder.build());
} else {
// copy file to download folder
File downloadDir = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
File newFile = new File(downloadDir, file.getName());
try {
FileInputStream inputStream = new FileInputStream(file);
FileOutputStream outputStream = new FileOutputStream(newFile);
FileChannel inChannel = inputStream.getChannel();
FileChannel outChannel = outputStream.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
inputStream.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
// send download complete notification
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
downloadManager.addCompletedDownload(
newFile.getName(),
newFile.getName(),
true,
getMimeType(Uri.fromFile(newFile), context),
newFile.getAbsolutePath(),
newFile.length(),
true
);
}
}