Android 如果SD卡中存在文件,则显示toast,并检查文件大小

Android 如果SD卡中存在文件,则显示toast,并检查文件大小,android,android-sdcard,Android,Android Sdcard,若SD卡中已经存在文件,则尝试显示toast,但条件似乎不适合我 public void startDownload(View v) { if(hasAudio.equals("no")) { Toast.makeText(getApplicationContext(), "MP3 not Available !", Toast.LENGTH_LONG).show(); } else if(hasAudio.equal

若SD卡中已经存在文件,则尝试显示toast,但条件似乎不适合我

public void startDownload(View v) {

     if(hasAudio.equals("no"))
        {
            Toast.makeText(getApplicationContext(), "MP3 not Available !", Toast.LENGTH_LONG).show();
        }
     else if(hasAudio.equals("yes"))             
        {
         File file = new File(getExternalCacheDir() + "Audios", title + ".mp3" );
         if (file.exists()) 
         {
           Toast.makeText(getApplicationContext(), "Already downloaded !", Toast.LENGTH_LONG).show();
         }
         else 
         {
            Uri uri=Uri.parse(download);
            mgr.enqueue(new DownloadManager.Request(uri)
                .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
                                        DownloadManager.Request.NETWORK_MOBILE)
                .setAllowedOverRoaming(false)
                .setTitle(title)
                .setDestinationInExternalPublicDir("Audios", title + ".mp3"));

            v.setEnabled(false);
          }         
     }
}

每当我点击按钮,它就会开始下载mp3,不管是否已经存在,但我想显示toast如果文件已经存在于SD卡中,那么为什么需要其他条件,请将其删除

public void startDownload(View v) {

 if(hasAudio.equals("no"))
    {
        Toast.makeText(getApplicationContext(), "MP3 not Available !", Toast.LENGTH_LONG).show();
    }
 else if(hasAudio.equals("yes"))             
    {
     File file = new File(getExternalCacheDir() + "Audios", title + ".mp3" );
     if (file.exists()) 
     {
       Toast.makeText(getApplicationContext(), "Already downloaded !", Toast.LENGTH_LONG).show();
     }else{
       Toast.makeText(getApplicationContext(), "Not Exist !", Toast.LENGTH_LONG).show();
     }


 }
}

您确定要检查
getExternalCacheDir()
而不是
getExternalFilesDir()
?我的建议是检查文件大小,而不仅仅是检查文件是否存在。因此,即使下载由于网络故障而中断(并且损坏的文件仍然存在),它也能工作。@SeshuVinay嘿,我同意你的观点,你能给我指路吗?看,这可能对你有帮助。也看到