【问题标题】:Android app lags while a big file is downloaded下载大文件时 Android 应用程序滞后
【发布时间】:2017-08-16 11:20:49
【问题描述】:

我在我的 android 应用程序中使用 Fast-Android-Networking 库通过它下载文件。

对于小于15 megabytes 的文件,我的代码可以正常工作。但是,当我尝试下载长度超过20 megabytes 的文件时,应用程序和目标设备都会滞后。

当我使用具有 512mb 内存的旧联想 A319 进行测试时,我发现了问题,并认为这一定是硬件问题。

但是,在 Lenovo A6000、Samsung J1 4G、摩托罗拉 Moto G Turbo 和 LYF Water 11 中测试了该应用程序后,我决定该应用程序在所有设备上都滞后,并且仅在 em> 正在下载文件。

我不明白为什么会出现这个问题。我还检查了 logcat,但没有发现任何东西可以帮助我理解问题的根源。

有什么想法吗?

我的一些代码:

    AndroidNetworking.download(link, Environment.getExternalStorageDirectory().getPath() + "/ABCD", title + "."+fileExtension)
            .setTag("Download: " + title)
            .setPriority(Priority.HIGH)
            .build()
            .setDownloadProgressListener(new DownloadProgressListener() {
                @Override
                public void onProgress(long bytesDownloaded, long totalBytes) {
                    int percentage = (int) Math.floor(bytesDownloaded * 100.0 / totalBytes);
                    System.out.println(percentage);
                    //I'm using Notification to report progress to user.
                    mBuilder.setProgress(100, percentage, false);
                    mNotifyManager.notify(id, mBuilder.build());
                }
            })
            .startDownload(new DownloadListener() {
                @Override
                public void onDownloadComplete() {
                    if (file.length() < 500000) {

                        Snackbar.make(mView, "Download Error", Snackbar.LENGTH_LONG).show();
                        mBuilder.setContentText("Download Error!").setProgress(0, 0, false);
                        mNotifyManager.notify(id, mBuilder.build());

                    } else {

                        Uri path = Uri.fromFile(file);
                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent.setDataAndType(path, getMimeType(path.toString()));

                        PendingIntent notifyPIntent = PendingIntent.getActivity(mContext.getApplicationContext(), 0, intent, 0);
                        mBuilder.setContentIntent(notifyPIntent);

                        mBuilder.setContentTitle("Download Completed")
                                .setContentText("Download complete! - Click To Play")
                                .setProgress(0, 0, false);
                        mNotifyManager.notify(id, mBuilder.build());

                        Snackbar.make(mView, "Download Completed : " + title + "."+fileExtension, Snackbar.LENGTH_LONG).setAction("Play Now", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Uri path = Uri.fromFile(file);
                                Intent intent = new Intent(Intent.ACTION_VIEW);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                intent.setDataAndType(path, getMimeType(path.toString()));
                                mContext.startActivity(intent);
                            }
                        }).show();

                    }
                }

                @Override
                public void onError(ANError error) {
                    Snackbar.make(mView, "Download Error : " + error.toString(), Snackbar.LENGTH_LONG).show();
                    error.printStackTrace();
                    mBuilder.setContentText("Download Error!");
                    mBuilder.setProgress(0, 0, false);
                    mNotifyManager.notify(id, mBuilder.build());
                }
            });

【问题讨论】:

    标签: java android file download android-networking


    【解决方案1】:

    基于this 问题,库在内存使用区域存在严重问题。

    【讨论】:

    • 你能给我一些建议吗? (没有 Koush 离子)
    • @Exploit 当然,你绝对需要避免使用这个库。
    • 虽然有一些问题,但我至少搬到了 ion。
    猜你喜欢
    • 2023-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 2021-08-28
    • 1970-01-01
    相关资源
    最近更新 更多