【问题标题】:How to get the downloaded file path using the download manager如何使用下载管理器获取下载的文件路径
【发布时间】:2018-05-29 13:23:18
【问题描述】:

我可以使用下载管理器从服务器下载视频。但是,当我使用以下代码记录路径时。

 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 保险.mp4

是什么带来了这种差异?如何才能以这种方式获得手机中的路径?

【问题讨论】:

  • 最好在下载文件夹中下载...dmr.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
  • 你是指Environment.DIRECTORY_DOWNLOADS中的默认文件夹
  • 是的@Rapheal 在那里下载文件
  • 谢谢@Singh 让我试试
  • 顺便说一句,你在 directorysubpath 中传递了什么...如果你想在默认下载文件夹中下载,我可以给你代码 sn-p

标签: android file path


【解决方案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);
}

【讨论】:

    【解决方案2】:

    代码 sn -p 下载默认下载目录中的文件。

    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};
    

    【讨论】:

    • 谢谢让我试试。并回复你
    • 下载到下载文件夹后,路径为/storage/sdcard0/Download/Soil Testing.mp4。这和我直接在手机上查看时的文件路径还是/storage/不同sdcard0/Android/data/com.ezyagric.extension.android/files/Download/Soil Testing.mp4 ........首先,这些路径应该是一样的
    • 文件应该在/storage/sdcard0/Download/下载
    • 这是我拥有的 /storage/sdcard0/Download/Soil Testing.mp4
    • 您可以检查文件是否存在... File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), nameOfFile); if(file.exists())
    【解决方案3】:

    您应该尝试在下载文件夹中下载

            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);
    

    【讨论】:

    • 让我试试,我会回复你的。否则感谢您的贡献
    • 欢迎您,乐于助人
    • 下载到下载文件夹后,路径为/storage/sdcard0/Download/Soil Testing.mp4。这和我直接在手机上查看时的文件路径还是/storage/不同sdcard0/Android/data/com.ezyagric.extension.android/files/Download/Soil Testing.mp4 ........首先,这些路径应该是一样的
    • 尝试使用 setDestinationInExternalFilesDir 而不是 setDestinationInExternalPublicDir
    • 现在让我这样做
    猜你喜欢
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    相关资源
    最近更新 更多