李堇 2023-10-25 13:45 采纳率: 52.8%
浏览 28
已结题

android下载失败

在应用中执行下载安装新版本程序时,有些设备总出现下载失败,进度条刚出现就因为异常自动关闭,请问哪位知道哪里出现问题了?
以下为相关代码和使用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

  • 写回答

19条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2023-10-25 13:56
    关注
    获得0.60元问题酬金

    引自强大而免费的微信小程序:皆我百晓生

    根据提供的代码和日志,下载失败的原因可能是以下几个方面:

    1. 网络连接异常:请确保设备正常连接网络,并且可以访问到下载地址。
    2. 存储空间不足:检查设备存储空间是否足够存储下载的文件。可以尝试清理设备内存或者切换存储位置。
    3. 文件路径错误:请确保下载的文件路径正确,即CommonConstants.DOWNLOAD_PATH + fileName
    4. 权限问题:检查是否授予应用程序所需的写入和网络权限。
    5. 异常处理问题:确保UpdateCallbackonFailure方法正常处理异常情况,以便能够及时停止进度条并进行相应的提示。

    以上是一些常见的问题和解决方法,具体问题还需要根据实际运行环境和情况进一步排查。

    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 11月2日
  • 赞助了问题酬金20元 10月25日
  • 创建了问题 10月25日

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号