下载文件和崩溃应用程序

时间:2015-03-17 18:16:44

标签: android crash android-4.4-kitkat

我的应用程序下载文件:

public class DownloadService extends IntentService {
public static final int UPDATE_PROGRESS = 8344;
public DownloadService() {
    super("DownloadService");
}
@Override
protected void onHandleIntent(Intent intent) {
    String urlToDownload = intent.getStringExtra("url");
    String fileToStore= intent.getStringExtra("file");
    ResultReceiver receiver = (ResultReceiver) intent.getParcelableExtra("receiver");
    try {
        URL url = new URL(urlToDownload);
        URLConnection connection = url.openConnection();
        connection.connect();
        // this will be useful so that you can show a typical 0-100% progress bar
        int fileLength = connection.getContentLength();

        // download the file
        InputStream input = new BufferedInputStream(connection.getInputStream());
        OutputStream output = new FileOutputStream("/sdcard/SingingStudio/"+fileToStore+".zip");

        byte data[] = new byte[1024];
        long total = 0;
        int count;
        while ((count = input.read(data)) != -1) {
            total += count;
            // publishing the progress....
            Bundle resultData = new Bundle();
            resultData.putInt("progress" ,(int) (total * 100 / fileLength));
            receiver.send(UPDATE_PROGRESS, resultData);
            output.write(data, 0, count);
        }

        output.flush();
        output.close();
        input.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Bundle resultData = new Bundle();
    resultData.putInt("progress" ,100);
    receiver.send(UPDATE_PROGRESS, resultData);
}
}

我的应用在任何设备中正常工作。但在所有kitkat版本的android中,例如xperia z4和galaxy grande 4.2.2 当文件下载。如果我去任何活动应用程序崩溃。 但如果下载带有DownloadManager应用程序的文件没有崩溃。

为什么?

0 个答案:

没有答案