Finding Android development to grind out every feature related to the system is not friendly to beginners. Bad luck is difficult to find effective information quickly and accurately 😒

This article is about how Android writes files to storage and displays them in the download list and recent file list of file management.

Suppose the saved File is File File in the external storage, either in the app private directory (not tested) or in the Download, Pictures, or other directories in the external storage root directory (no problem found).

The test supports Android 4.0-9.0.

Step one, throw the violence to the media scan, whether you’re a picture or not

If our files are pictures, videos, music and other media files, display to albums and other places

context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
Copy the code

The second step is to add it to the download list, which automatically displays the latest files

String mime=MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext) || ||"application/octet-stream";// Use the last one, how to write fast?

DownloadManager manager= (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
manager.addCompletedDownload(file.getName(), file.getName(), true, mime, file.getAbsolutePath(), file.length(),false);
Copy the code

Real effective ingredient: DownloadManager addCompletedDownload, calls will add files to the download list, and appear in the recent documents list (pictures are for other types of testing may be). When you want this function, you may not be lucky enough to find it.


end.