I am able to download a video from the server using the download manager. However when I log the path using below code.
String path = Environment.getExternalStoragePublicDirectory(directory).getAbsolutePath() + subpath;
Log.e("PATH", path);
I get
12-15 13:29:36.787 22807-22807/com.ezyagric.extension.android E/PATH: /storage/sdcard0/EZYAGRIC/Soil Testing.mp4.
Now this is different from the path on the phone which is
/storage/sdcard0/Android/data/com.ezyagric.extension.android/files/EZYAGRIC/Crop Insurance.mp4
What brings that difference and how can obtain the path in the phone the way it is?
Code snippet to download file in default download directory.
DownloadManager.Request dmr = new DownloadManager.Request(Uri.parse(url));
// If you know file name
String fileName = "filename.xyz";
//Alternative if you don't know filename
String fileName = URLUtil.guessFileName(url, null,MimeTypeMap.getFileExtensionFromUrl(url));
dmr.setTitle(fileName);
dmr.setDescription("Some descrition about file"); //optional
dmr.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
dmr.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
dmr.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
DownloadManager manager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(dmr);
Note For mContext.getSystemService
getSystemService();
getActivity.getSystemService();
mContext.getSystemService(); //pass context in adapter
UPDATE
As OP want to check file exist or not
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);
if(file.exists()){//File Exists};
You should try to download in download folder
String url = "url you want to download";
DownloadManager.Request request = new
DownloadManager.Request(Uri.parse(url));
request.setDescription("Some descrition");
request.setTitle("Some title");
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "name-of-the-file.ext");
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With