I try to download audio from a URL. I use following sample download manager code from Github https://github.com/folee/Download_Mgr
My problem is If i cancel download, Incomplete mp3 file still there in SDcard. How can i remove Incomplete mp3 files?
@Override
public void onClick(View v) {
Intent downloadIntent = new Intent(DownloadValues.Actions.DOWNLOAD_SERVICE_ACTION);
switch (v.getId()) {
case R.id.btn_continue:
// mDownloadManager.continueTask(mPosition);
downloadIntent.putExtra(DownloadValues.TYPE, DownloadValues.Types.CONTINUE);
downloadIntent.putExtra(DownloadValues.URL, url);
mContext.startService(downloadIntent);
mViewHolder.continueButton.setVisibility(View.GONE);
mViewHolder.pauseButton.setVisibility(View.VISIBLE);
break;
case R.id.btn_pause:
// mDownloadManager.pauseTask(mPosition);
downloadIntent.putExtra(DownloadValues.TYPE, DownloadValues.Types.PAUSE);
downloadIntent.putExtra(DownloadValues.URL, url);
mContext.startService(downloadIntent);
mViewHolder.continueButton.setVisibility(View.VISIBLE);
mViewHolder.pauseButton.setVisibility(View.GONE);
break;
case R.id.btn_delete:
// mDownloadManager.deleteTask(mPosition);
downloadIntent.putExtra(DownloadValues.TYPE, DownloadValues.Types.DELETE);
downloadIntent.putExtra(DownloadValues.URL, url);
mContext.startService(downloadIntent);
removeItem(url);
break;
}
}
my file bath :
public class ConfigUtils {
public static void InitPath(Context ctx) {
File temFile = Environment.getExternalStorageDirectory();
if (temFile != null && temFile.canWrite() && Util.getAvailableExternalMemorySize() > 0) {
IMG_PATH = Environment.getExternalStorageDirectory().getPath() + "/DL_Mgr/Image/";
FILE_PATH = Environment.getExternalStorageDirectory().getPath() + "/DL_Mgr/Downloads/";
}
else {
IMG_PATH = ctx.getFilesDir() + "/Image/";
FILE_PATH = ctx.getFilesDir() + File.separator;
}
new File(IMG_PATH).mkdirs();
new File(FILE_PATH).mkdirs();
Log.i(TAG, "IMG_PATH-->" + IMG_PATH + "\n FILEPATH-->" + FILE_PATH);
}
}
I use this code for delete temp files but not work
case R.id.btn_delete:
File tempFile = new File(ConfigUtils.FILE_PATH + "filename".toString());
if(tempFile.exists()) {
tempFile.delete();
}
But it not work EDIT: WORKING CODE: (If download mp3 temp file remane like : "sample.mp3.download"
so ichanged code like this it work fine
File tempFile = new File(ConfigUtils.FILE_PATH + filename +".download".toString());
if(tempFile.exists()) {
tempFile.delete();
}
Before downloading starts. You must know where you are saving the file. So probably a absolute path or Uri pointing to a file in sdcard that you are giving to Download manager to store your file at.
Incase of cancel. create a file object from that uri or absolute path and call delete on file object if exists. Something like this
File tempFile = new File(uri.toString());
if(tempFile.exists()) {
tempFile.delete();
}
This should be simple. Google it out