在应用中执行下载安装新版本程序时,有些设备总出现下载失败,进度条刚出现就因为异常自动关闭,请问哪位知道哪里出现问题了?
以下为相关代码和使用adb抓取的日志:
//String url = "http://res.minxueedu.cn/res/prod/SSEP/uploadfiles/apk/"+fileName;
UpdateService.download(fileName,apkUpdateUrl,UpdateActivity.this, new UpdateService.UpdateCallback() {
@Override
public void onSuccess() {
dialog.dismiss();
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return;
}
File file = new File(CommonConstants.DOWNLOAD_PATH + fileName);
try {
/*System.out.println("安装文件目录:" + file);
System.out.println("准备安装");*/
installApk(file);
} catch (Exception e) {
Log.e("获取打开方式错误", e.getMessage());
}
}
@Override
public void onProgress(int progress) {
dialog.setProgress(progress);
}
@Override
public void onFailure() {
dialog.dismiss();
}
});
public class UpdateService {
private static OkHttpClient okHttpClient;
public static void download(final String fileName, String url, Context context, final UpdateCallback callback) {
Request request = new Request.Builder()
.addHeader("Accept-Encoding", "identity")
.url(url).build();
if (okHttpClient == null) {
okHttpClient = new OkHttpClient();
}
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
callback.onFailure();
ToastUtil.showShort(context,e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.body() == null) {
callback.onFailure();
return;
}
File filePath = new File(CommonConstants.DOWNLOAD_PATH);
if (!filePath.exists()) {
filePath.mkdirs();
}
long contentLength = response.body().contentLength();
byte[] buffer = new byte[4096];
File file = new File(filePath.getCanonicalPath(), fileName);
try (InputStream is = response.body().byteStream();
FileOutputStream fos = new FileOutputStream(file)) {
//Log.d("保存路径:" , file.getAbsolutePath());
int length;
long sum = 0;
while ((length = is.read(buffer)) != -1) {
fos.write(buffer, 0, length);
sum += length;
int progress = (int) (sum * 1.0f / contentLength * 100);
callback.onProgress(progress);
}
fos.flush();
callback.onSuccess();
} catch (Exception e) {
callback.onFailure();
}
}
});
}
public interface UpdateCallback {
void onSuccess();
void onProgress(int progress);
void onFailure();
}
}
10-25 10:03:12.255 I/gralloc4( 212): use RK_GRALLOC_USAGE_WITHIN_4G
10-25 10:03:12.256 D/gralloc4( 212): [File] : hardware/rockchip/libgralloc/bifrost/src/hidl_common/Allocator.cpp; [Line] : 149; [Func] : allocate;
10-25 10:03:12.256 D/gralloc4( 212): got new private_handle_t instance @0xb40000712972b190 for buffer '正在下载#0'. share_fd : 8, share_attr_fd : 9, flags : 0x4, width : 928, height : 261, req_format : 0x1, producer_usage : 0x40000000000b00, consumer_usage : 0x40000000000b00, internal_format : 0x0, stride : 0, byte_stride : 0, internalWidth : 0, internalHeight : 0, alloc_format : 0x1, size : 968832, layer_count : 1, backing_store_size : 968832, backing_store_id : 910533066973, allocating_pid : 212, ref_count : 1, yuv_info : 0
10-25 10:03:12.256 D/gralloc4( 212): plane_info[0]: offset : 0, byte_stride : 3712, alloc_width : 928, alloc_height : 261
10-25 10:03:12.256 D/gralloc4( 212): plane_info[1]: offset : 0, byte_stride : 0, alloc_width : 0, alloc_height : 0