【问题标题】:Check If File Exists Before downloading the file下载文件前检查文件是否存在
【发布时间】:2018-06-21 08:30:43
【问题描述】:

我正在使用下载管理器下载文件。下载文件的代码如下。

   private String DownloadData(Uri uri, View v, String textview) {

    long downloadReference;

    // Create request for android download manager
    dm = (DownloadManager)getContext().getSystemService(DOWNLOAD_SERVICE);
    DownloadManager.Request request = new DownloadManager.Request(uri);

    //Setting title of request
    request.setTitle(textview);

    //Setting description of request
    request.setDescription("Android Data download using DownloadManager.");

    //Set the local destination for the downloaded file to a path within the application's external files directory
    request.setDestinationInExternalFilesDir(getContext(), DIRECTORY_DOWNLOADS, File.separator + "Dr_Israr_Ahmad" + File.separator + textview+".mp3");

    //Enqueue download and save into referenceId
    downloadReference = dm.enqueue(request);

    return null
}

上面的代码工作正常。我现在需要做的是,如果文件已经下载而不是我希望我的应用程序播放它。使用的代码是

   String path = String.valueOf(getContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS+ File.separator+"Dr_Israr_Ahmad" + File.separator +filename+".mp3"));

                File file = new File(path);

                if(file.exists()){
                    Toast.makeText(getContext(),path+ "/n exists", Toast.LENGTH_SHORT).show();
                } else if (!file.exists()) {
                    Toast.makeText(getContext(), "Downloading", Toast.LENGTH_SHORT).show();
                   Uri uri = Uri.parse("http://www.digitalsguide.com/mobile-apps/dr-israr-ahmad/audios/"+filename+".mp3");
                   String filepath = DownloadData(uri,view,filename);
                }

但问题是即使文件不存在条件为真。我的路径有问题吗?请帮帮我,

【问题讨论】:

    标签: android file android-download-manager


    【解决方案1】:

    试着把你的路径像下面的例子:

    File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+ "/" +filename);
    

    这里的文件名是example.pdf 然后你可以检查文件是否存在

    【讨论】:

      【解决方案2】:

      因为getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) 返回/storage/emulated/0/Android/data/<PACKAGE_ID>/files/Download。当我们设置Environment.DIRECTORY_DOWNLOADS时,它不是DownloadManager下载文件的文件夹。

      【讨论】:

        【解决方案3】:

        .getExternalFilesDir(yourFilePath) 在您的代码中创建一个目录。所以像这样使用它。

        String path = String.valueOf(getContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)+ File.separator+"Dr_Israr_Ahmad" + File.separator +filename+".mp3");
        

        【讨论】:

        • 这没有回答所提出的问题。
        【解决方案4】:

        我之前检测到exists 的一些奇怪行为并将其更改为isFile

        File file = new File(path);
        if (file.isFile()) {
            Toast.makeText(getContext(), path + "/n exists", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getContext(), "Downloading", Toast.LENGTH_SHORT).show();
            // ...
        }
        

        我认为每次执行new File() 时,手机都会以某种方式创建一个目录。 检查一下。

        【讨论】:

        • 这个解决方案是本地检查文件是否存在,但如果需要远程检查文件是否存在呢?
        • @leeCoder - 然后你问一个新问题,或者搜索一个现有的问题来回答它。因为那不是>这个
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-03-07
        • 2013-12-21
        • 1970-01-01
        • 1970-01-01
        • 2013-02-01
        • 1970-01-01
        相关资源
        最近更新 更多