当我点击按钮时,正确下载文件。但再次点击按钮再次下载,但我不想再次下载文件和显示警报消息是下载的文件 我该怎么办?
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 :(得分: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();
}
}
});
答案 1 :(得分: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();
}