How to save a downloaded file on an SD card from the program manager programmatically on android

In my application, I have a download (image) function that is used to download a file from URLs. The download should be shown in the notification panel so that I use the Download Manager class to download the file. This works fine, but the downloaded image is not stored on the SD card.

I sent a url to the download manager.

My requirement is that I need to save the download image to an SD card with an indication of the notification bar. What you need to change to the code in order to get the image with saving to the SD card at the above link

I have some doubts about the code in the link above. Can I use the same code to upload audio or video files?

please help me.

Edited Question:

I tried

filepath = Environment.getExternalStorageDirectory().getPath()+"/download/cm.png"; Uri destinationUri = Uri.parse(filepath); request.setDestinationUri(destinationUri); 

before pressing the preference button. but I could not get the file on the SD card.

+4
source share
3 answers

This is what I used.

  Uri downloadUri = Uri.parse(DOWNLOAD_FILE); DownloadManager.Request request = new DownloadManager.Request(downloadUri); request.setDescription("Downloading a file"); long id = downloadManager.enqueue(request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |DownloadManager.Request.NETWORK_MOBILE) .setAllowedOverRoaming(false) .setTitle("File Downloading...") .setDescription("Image File Download") .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "cm.png")); 
+12
source

In the code you link to, the file opens at the end. At this point, you might consider copying it to an SDCard.

Otherwise (better) use http://developer.android.com/reference/android/app/DownloadManager.Request.html setDestinationUri (android.net.Uri) to indicate where you want to download the file.

+2
source
  downloadmanager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); Toast.makeText(context, "Downloading...", Toast.LENGTH_LONG).show(); Uri uri = Uri.parse("---- url here ------"); request = new DownloadManager.Request(uri); request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE); request.setAllowedOverRoaming(false); request.setTitle("---- title here ------"); String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl("---- url here ------"); request.setMimeType(mimeType); request.setDescription("---- descripation here ------"); if("---- titlehere ------" != null){ request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "---- title here ------"); } request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); Long reference = downloadmanager.enqueue(request); 
0
source

Source: https://habr.com/ru/post/1433727/


All Articles