0

Currently

I am downloading a file from a URL, I'm doing this within AsyncTask inside a Adapter. The problem I have is that when I press back onBackPressed the download stops but the file remains in the folder FileOutputStream(Environment.getExternalStorageDirectory().toString()+"/file.mp4");


My Question

Is it possible to delete the file if AsyncTask does not complete?


I have tried to do file.delete(); in the catch of doinbackground but I get error file.delete(); is ignored

Here is a summary of my adapter----

When Item in holder is clicked I call AsyncTask:

holder.setItemClickListener(new ItemClickListener() {
    if (pos == 1) {
                if(manager.fetchVideoPath(pos)==null) {

                    DownloadFileFromURL p = new DownloadFileFromURL();                  
                    p.execute(pos + "", "https://www.dropbox.com/s/xnzw753f13k68z4/Piper%20First%20Look%20%282016%29%20-%20Pixar%20Animated%20Short%20HD.mp4?dl=1");
                    a = "one";
                    bars.set(pos,new ProgressModel(pos,1));
                    //This is what is causing the issue
                    RecyclerVideoAdapter.this.notifyItemChanged(pos);
}

My AsyncTask:

private class DownloadFileFromURL extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
    super.onPreExecute();
    ((CircularProgressBar)vview.findViewById(R.id.circularProgressBar)).setProgress(1);
}

@Override
    protected String doInBackground(String... f_url) {
        int count;
        String pathreference = f_url[0]+",";
        positionnumber = Integer.parseInt(f_url[0]);
        try {
            URL url = new URL(f_url[1]);
            URLConnection conection = url.openConnection();
            conection.connect();

            int lenghtOfFile = conection.getContentLength();

            InputStream input = new BufferedInputStream(url.openStream(),
                    8192);

            if (a.equals("one")) {
                OutputStream output = new FileOutputStream(Environment
                        .getExternalStorageDirectory().toString()
                        + "/file.mp4");


                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();
                pathreference = pathreference+Environment.getExternalStorageDirectory().toString()+"/file.mp4";
                output.close();
                input.close();




            }
        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }

        return pathreference;
    }

protected void onProgressUpdate(String... progress) {           
        bars.get(positionnumber).setProgress_(Float.parseFloat(progress[0]));
        ((CircularProgressBar)vview.findViewById(R.id.circularProgressBar)).setProgress(bars.get(positionnumber).getProgress_());
    }

@Override
    protected void onPostExecute(String file_url) {
        String []split = file_url.split(",");
        int index1 = Integer.parseInt(split[0]);
        videoHolderClass.set(index1,new VideoHolderClass(index1,imgres[0]));
        bars.get(index1).setProgress_(0);
        manager.insertVideoPath(index1+"",split[1]);
        RecyclerVideoAdapter.this.notifyItemChanged(index1);

    }

}
ClassA
  • 2,480
  • 1
  • 26
  • 57

1 Answers1

0

Building on the answer on this post, try to put the logic of deleting the file inside isCancelled() like this:

if (isCancelled() && file.exists()) 
    file.delete();
else
{
   // do your work here
}

Then you can call p.cancel(true) inside onBackPressed

AhmedAbdelaal
  • 286
  • 2
  • 6