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

时间:2014-03-18 07:41:22

标签: android android-sdcard

如果文件已存在于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 
         {
            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,无论是否存在,但我想要显示吐司文件已存在于SD卡中

1 个答案:

答案 0 :(得分:0)

那为什么你需要其他条件,删除它

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


 }
}