如果AsyncTask失败,则删除文件

时间:2017-07-18 16:29:30

标签: android android-asynctask android-adapter

目前

我正在从网址下载文件,我在AsyncTask内的Adapter内执行此操作。我遇到的问题是,当我按下onBackPressed时,下载停止,但文件仍保留在文件夹FileOutputStream(Environment.getExternalStorageDirectory().toString()+"/file.mp4");

我的问题

如果AsyncTask没有完成,是否可以删除文件?

我已尝试file.delete(); catch doinbackground但我收到错误file.delete(); is ignored

以下是我的适配器----

的摘要

点击持有人中的项目后,我拨打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);
}

我的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);

    }

}

1 个答案:

答案 0 :(得分:0)

根据this帖子的答案,尝试将删除文件的逻辑放在isCancelled()内,如下所示:

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

然后,您可以在p.cancel(true)

内拨打onBackPressed