我的代码问题是什么:
@Override
protected String doInBackground(String... sUrl) {
InputStream input = null;
URLConnection conection = null;
BufferedOutputStream bout = null;
FileOutputStream fos = null;
int downloaded = 0;
try {
URL url = new URL(sUrl[0]);
conection = url.openConnection();
//conection.connect();
int lenghtOfFile = conection.getContentLength();
conection = null;
conection = url.openConnection();
if(STATUS) {
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/myapp.apk");
if (file.exists()) {
downloaded = (int) file.length();
conection.setRequestProperty("Range", "bytes=" + (file.length()) + "-");
}
}
else {
conection.setRequestProperty("Range", "bytes=" + downloaded + "-")
}
conection.setDoInput(true);
conection.setDoOutput(true);
input = new BufferedInputStream(url.openStream(), 8192);
fos=(downloaded==0)? new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/myapp.apk"): new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/myapp.apk",true);
bout = new BufferedOutputStream(fos, 1024);
byte data[] = new byte[1024];
long total = 0;
int count = 0;
while ((count = input.read(data, 0, 1024)) >= 0) {
if (isCancelled()) {
input.close();
return null;
}
bout.write(data, 0, count);
downloaded += count;
publishProgress((int)(downloaded * 100/ lenghtOfFile) );
total += count;
}
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
} finally {
try {
if (output != null)
output.close();
if (input != null)
input.close();
if (fos != null)
fos.close();
if (bout != null)
bout.close();
} catch (IOException ignored) {
}
if (conection != null)
conection = null;
}
return null;
}
并在点击它时将此启动器设置为通知栏:
notification = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.down_icon)
.setOngoing(true);
resultIntent = new Intent(MainActivity.this, DownloadsActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(MainActivity.this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(resultPendingIntent);
当文件完全下载并点击通知栏时,我看到了这个错误:
There is a problem parsing the package
我的代码问题是什么?
提前致谢
答案 0 :(得分:0)
您是否尝试在while循环后添加bout.flush()?也许并非所有字节都写入您的文件