2014-02-05 74 views
0

當我點擊按鈕,下載文件是正確的。但再次點擊按鈕再次下載,但我不想再次下載文件,並顯示警報與消息是,下載的文件 我能做什麼?沒有下載文件,如果它存在它在android

public class Main extends Activity { 
Button btnShowProgress; 
ImageView my_image; 
private ProgressDialog pDialog; 
public static final int progress_bar_type = 0; 
private static String file_url = "http://dl.esfandune.ir/android/esfandune.jpg"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    btnShowProgress = (Button) findViewById(R.id.btnProgressBar); 

    my_image = (ImageView) findViewById(R.id.my_image); 
    btnShowProgress.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      new DownloadFileFromURL().execute(file_url); 
     } 
    }); 
} 

@Override 
protected Dialog onCreateDialog(int id) { 
switch (id) { 
case progress_bar_type: 
    pDialog = new ProgressDialog(this); 
    pDialog.setMessage("در حال دانلود تصویر...لطفا صبر کنید"); 
    pDialog.setIndeterminate(false); 
    pDialog.setMax(100); 
    pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
    pDialog.setCancelable(true); 
    pDialog.show(); 
    return pDialog; 
default: 
    return null; 
} 
} 

class DownloadFileFromURL extends AsyncTask<String, String, String> { 


@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    showDialog(progress_bar_type); 
} 


@Override 
protected String doInBackground(String... f_url) { 
    int count; 
    try { 
     URL url = new URL(f_url[0]); 
     URLConnection conection = url.openConnection(); 
     conection.connect(); 
     int lenghtOfFile = conection.getContentLength(); 
     InputStream input = new BufferedInputStream(url.openStream(), 8192); 
     OutputStream output = new FileOutputStream("/sdcard/downloadedfile.jpg"); 
     byte data[] = new byte[1024]; 
     long total = 0; 
     while ((count = input.read(data)) != -1) { 
      total += count; 

      publishProgress(""+(int)((total*100)/lenghtOfFile)); 


      output.write(data, 0, count); 
     } 


     output.flush(); 


     output.close(); 
     input.close(); 

    } catch (Exception e) { 
     Log.e("Error: ", e.getMessage()); 
    } 

    return null; 
} 


    protected void onProgressUpdate(String... progress) { 

    pDialog.setProgress(Integer.parseInt(progress[0])); 
} 

@SuppressWarnings("deprecation") 
@Override 
protected void onPostExecute(String file_url) { 

    dismissDialog(progress_bar_type); 

    String imagePath = Environment.getExternalStorageDirectory().toString() + "/downloadedfile.jpg"; 

    my_image.setImageDrawable(Drawable.createFromPath(imagePath)); 
} 
}} 

回答

0

變化

btnShowProgress.setOnClickListener(new View.OnClickListener() 
{ 

    @Override 
    public void onClick(View v) 
    { 
     new DownloadFileFromURL().execute(file_url); 
    } 
}); 

btnShowProgress.setOnClickListener(new View.OnClickListener() 
{ 

    @Override 
    public void onClick(View v) 
    { 
    if(!new File(Environment.getExternalStorageDirectory().toString() + "/downloadedfile.jpg").exists())   
     new DownloadFileFromURL().execute(file_url); 
    else 
    { 
     AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(Main.this);      
     dialogBuilder.setMessage("This file has already been downloaded")    
     AlertDialog dialog = dialogBuilder.create(); 
     dialog.show(); 
    } 
    } 
}); 
0

之前執行DownloadFileFromURL,您可以檢查文件是否存在使用

​​
相關問題