AsyncTask下载文件与0B



我试图用AsyncTask下载文件,所有下载的文件都有0b。我不知道该怎么办。有人能帮忙吗?请帮帮我,我为此花了一整天的时间。我错在哪里?我从来没有使用过AsyncTask,我用例子写了这个代码。

一些代码:

public class DownloadProgramTask extends AsyncTask<Void,Void,Void> {
    int id;
    MYDBHelperFromApi mydbHelperFromApi;
    Program program;
    List<Asana> asanaList;
    public DownloadProgramTask(int id, MYDBHelperFromApi mydbHelperFromApi){
       this.id = id;
       this.mydbHelperFromApi = mydbHelperFromApi;
    }
    @Override
    protected Void doInBackground(Void...params) {
       program = mydbHelperFromApi.getProgramForStart(id);
       asanaList = program.getAsanasList();
        final String FILE_PATH_PROGRAM_VOICE = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Programs/Voice/"+id+"/";
        List<String> voiceList = new ArrayList<>();
        List<File> fileList = new ArrayList<>();
        File dirVoice = new File(FILE_PATH_PROGRAM_VOICE);
        fileList.add(dirVoice);
        for (File f:fileList) {
            if (!f.exists())
                f.mkdirs();
        }
        for (Asana a:asanaList) {
            File voiceFile = new File(dirVoice, a.getPose_id() + ".mp3");
            if (!voiceFile.exists()) {
                voiceList.add(a.getVoice());
                download(MYurl.BASE_URL + a.getVoice(), voiceFile.getPath());
                Log.e("LINK",MYurl.BASE_URL + a.getVoice());
                Log.e("Path voice", "" + voiceFile.getPath());
            }
        }
         return null;
    }
    public void download(String s, String Path_file) {
        InputStream input = null;
        OutputStream output = null;
        HttpURLConnection connection = null;
        try {
            URL url = new URL(s);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
               Log.d("Something went wrong"," "+s);
            }

            int fileLength = connection.getContentLength();

            input = connection.getInputStream();
            output = new FileOutputStream(Path_file);
            byte data[] = new byte[4096];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                if (isCancelled()) {
                    input.close();
                }
                total += count;
            }
        } catch (Exception e) {
            Log.e("Exception", e.toString());
        } finally {
            try {
                if (output != null)
                    output.close();
                if (input != null)
                    input.close();
            } catch (IOException ignored) {
            }
            if (connection != null)
                connection.disconnect();
        }
    }
    }

你在哪里写in输出流?

在下载方法的while循环中添加这一行:

outputStream.write(data, 0, count);

在关闭FileOutputStream之前是否尝试过刷新?

试着把output.flush();放在output.close();前面

try {
  if (output != null) {
    output.flush();
    output.close();
  } 
  if (input != null)
    input.close();
} catch (IOException ignored) {
}

最新更新