我可以使用下载管理器从服务器下载视频。但是,当我使用下面的代码记录路径时。
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
是什么带来了这种差异,以及如何以手的方式获得手机中的路径?
答案 0 :(得分:2)
要在默认下载目录中下载文件的代码段。
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
<强>更新强>
因为OP想检查文件是否存在
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), fileName);
if(file.exists()){//File Exists};
答案 1 :(得分:1)
您应该尝试在下载文件夹中下载
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);
答案 2 :(得分:1)
在调用-> 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);
}