[英]How to get the downloaded file path using the download manager
我可以使用下載管理器從服務器下載視頻。 但是,當我使用以下代碼記錄路徑時。
String path = Environment.getExternalStoragePublicDirectory(directory).getAbsolutePath() + subpath;
Log.e("PATH", path);
我得到
12-15 13:29:36.787 22807-22807/com.ezyagric.extension.android E/PATH:/storage/sdcard0/EZYAGRIC/Soil Testing.mp4。
現在這與手機上的路徑不同
/storage/sdcard0/Android/data/com.ezyagric.extension.android/files/EZYAGRIC/Crop Insurance.mp4
是什么帶來了這種差異,如何才能獲得手機中的路徑?
用於在默認下載目錄中下載文件的代碼片段。
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);
注意對於mContext.getSystemService
getSystemService();
getActivity.getSystemService();
mContext.getSystemService(); //pass context in adapter
mContext.getSystemService(); //pass context in adapter
更新
由於 OP 想要檢查文件是否存在
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);
if(file.exists()){//File Exists};
您應該嘗試在下載文件夾中下載
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);
當您在設置路徑之前調用 --> downloadManager.enqueue(request)方法時
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"Google.gif");
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"google.gif"); // Set Your File Name
if (file.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
pro28_imageView.setImageBitmap(myBitmap);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.