2014-03-03 90 views
-1

我在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); 
}}} 

回答

0

我不確定什麼是怎麼回事,但什麼威力幫助在該檢查的末尾添加一個finally{}塊如果讀取的總字節數等於文件的長度。如果不是,請刪除文件。

0

這是因爲你永遠不會取消AsyncTask!您需要在AsyncTask對象上調用cancel(),但要做到這一點,您需要先將實例保留在變量中。

因此,首先,保持的AsyncTask的實例,所以在你的類聲明的任務

DownloadFileFromURL downloadTask; 

當你創建任務,它分配給您的變量

downloadTask = new DownloadFileFromURL().execute(file_url); 

,只要你想取消,請撥打:

downloadTask.cancel(); 
+0

你一定是在開玩笑! – Merlevede

相關問題