2014-02-05 5 views
0

버튼을 클릭하면 파일이 제대로 다운로드됩니다. 다시 다운로드 버튼을 다시 클릭하지만 다시 파일을 다운로드하고 경고 메시지를 표시하고 싶지는 않습니다. 메시지가 다운로드 한 파일이므로 무엇을 할 수 있습니까?안드로이드에 파일이있는 경우 파일을 다운로드하지 않습니다.

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을 실행하기 전에, 당신은 파일이 존재 또는

File file = new File("/sdcard/downloadedfile.jpg"); 
if (!file.exists()){ 
    new DownloadFileFromURL().execute(file_url); 
} 
else{ 
    Toast toast = Toast.makeText(getApplicationContext(), "File Already Exists !!", Toast.LENGTH_SHORT).show(); 
} 
를 사용할지 여부를 확인할 수 있습니다
관련 문제