如果android中存在文件,则不下载
当我点击按钮时,下载文件是正确的。但再次点击按钮再次下载,但我不想再次下载文件,并显示警告信息是,下载的文件 我能做什么如果android中存在文件,则不下载,android,android-download-manager,Android,Android Download Manager,当我点击按钮时,下载文件是正确的。但再次点击按钮再次下载,但我不想再次下载文件,并显示警告信息是,下载的文件 我能做什么 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_ur
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));
}
}}
public类主扩展活动{
按钮BTN显示进度;
图像查看我的图像;
私人对话;
公共静态最终整数进度条类型=0;
私有静态字符串文件\u url=”http://dl.esfandune.ir/android/esfandune.jpg";
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnShowProgress=(按钮)findViewById(R.id.btnProgressBar);
my_image=(ImageView)findViewById(R.id.my_image);
btnShowProgress.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
新建DownloadFileFromURL().execute(文件url);
}
});
}
@凌驾
受保护的对话框onCreateDialog(int id){
开关(id){
案例进度条类型:
pDialog=新建进度对话框(此对话框);
pDialog.setMessage(“ددددددددددددددددددددیید…”;
pDialog.setUndeterminate(假);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_水平);
pDialog.setCancelable(真);
pDialog.show();
返回pDialog;
违约:
返回null;
}
}
类DownloadFileFromURL扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
显示对话框(进度条类型);
}
@凌驾
受保护的字符串doInBackground(字符串…f_url){
整数计数;
试一试{
URL=新URL(f_URL[0]);
URLConnection conconnection=url.openConnection();
conconnect.connect();
int lenghtOfFile=conconnect.getContentLength();
InputStream输入=新的BufferedInputStream(url.openStream(),8192);
OutputStream output=新文件OutputStream(“/sdcard/downloaddedfile.jpg”);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
出版进度(“+(int)((总计*100)/长度文档));
输出.写入(数据,0,计数);
}
output.flush();
output.close();
input.close();
}捕获(例外e){
Log.e(“错误:,e.getMessage());
}
返回null;
}
受保护的void onProgressUpdate(字符串…进度){
pDialog.setProgress(Integer.parseInt(progress[0]));
}
@抑制警告(“弃用”)
@凌驾
受保护的void onPostExecute(字符串文件\u url){
解雇对话框(进度条类型);
字符串imagePath=Environment.getExternalStorageDirectory().toString()+“/downloadedfile.jpg”;
my_image.setImageDrawable(Drawable.createFromPath(imagePath));
}
}}
更改
btnShowProgress.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
new DownloadFileFromURL().execute(file_url);
}
});
到
在执行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();
}
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();
}