Question

When I click on the button, Download the file is properly. but click again button again downloaded but I do not want again download file and show alert with The message is that the downloaded file what can i do?

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));
 }
 }}
Was it helpful?

Solution

Change

btnShowProgress.setOnClickListener(new View.OnClickListener() 
{

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

to

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

OTHER TIPS

Before executing the DownloadFileFromURL , you can check whether the file exists or not using

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top