我在asynctask中使用onCancelled()刪除不完整的文件,但是當下載不完整的文件不刪除它時。 (文件是音樂)爲什麼不刪除asynctask中的不完整文件
爲什麼不在以下代碼中刪除不完整的文件?
我的API是8
這是我的代碼:
public class ZiaratMatn4 extends Activity implements OnClickListener {
MediaPlayer mp;
ImageButton btndownziarat;
ImageButton btnplayziarat;
SeekBar seek_bar;
Handler seekHandler = new Handler();
private ProgressDialog pDialog;
public static final int progress_bar_type = 0;
private static String file_url = "http://upir.ir/files92be/2eda2a6a5434.mp3";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ziaratmatn);
mp = MediaPlayer.create(this,Uri.fromFile(audioFile));
btnplayziarat = (ImageButton) findViewById(R.id.btnplayziarat);
btnplayziarat.setOnClickListener(this);
btndownziarat = (ImageButton) findViewById(R.id.btndownziarat);
btndownziarat.setOnClickListener(this);
getInit();
seekUpdation();
}
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
File sdcard = Environment.getExternalStorageDirectory();
File audioFile = new File(sdcard.getPath() + "/EBKH/basem-vares.mp3");
public void getInit() {
if(audioFile.exists())
{
seek_bar = (SeekBar) findViewById(R.id.sbziarat);
seek_bar.setMax(mp.getDuration());
}}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btnplayziarat :
if(audioFile.exists())
{
if(mp!=null)
{
if(mp.isPlaying())
{
mp.pause();
btnplayziarat.setImageResource(R.drawable.play);
}
else
{
mp.start();
btnplayziarat.setImageResource(R.drawable.puse);
}}}
break;
case R.id.btndownziarat :
if(!new File(Environment.getExternalStorageDirectory().toString() + "/EBKH/basem-vares.mp3").exists())
new DownloadFileFromURL().execute(file_url);
break;
}}
Runnable run = new Runnable() {
@Override
public void run() {
seekUpdation();
}
};
public void seekUpdation() {
if(audioFile.exists())
{
seek_bar.setProgress(mp.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
}}
@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 onCancelled() {
File file= new File("/sdcard/EBKH/basem-vares.mp3");
file.delete();
}
@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/EBKH/basem-vares.mp3");
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]));
}
@Override
protected void onPostExecute(String file_url) {
dismissDialog(progress_bar_type);
}}}
你一定是在開玩笑! – Merlevede