简体   繁体   English

下载大文件时Android应用程序滞后

[英]Android app lags while a big file is downloaded

I'm using Fast-Android-Networking library in my android app to download file through it. 我在我的Android应用中使用Fast-Android-Networking库通过它下载文件。

For files less than 15 megabytes , my code works fine. 对于小于15 megabytes文件,我的代码可以正常工作。 But, when I try downloading files with more than 20 megabytes of length, the application and also the target device lags. 但是,当我尝试下载长度超过20 megabytes的文件时,应用程序以及目标设备都会出现延迟。

When I tested with my old Lenovo A319 which has 512mb of RAM, I've found the problem and thought it must be an hardware problem. 当我对具有512mb RAM的旧联想A319进行测试时,我发现了问题,并认为这一定是硬件问题。

But, after testing the application in Lenovo A6000, Samsung J1 4G, Motorola Moto G Turbo and LYF Water 11, I came to this decision that the application is lagging on all devices and only while the file is being downloaded. 但是,在Lenovo A6000,Samsung J1 4G,Motorola Moto G Turbo和LYF Water 11中测试了该应用程序后,我决定该应用程序在所有设备上均处于滞后状态,并且仅在文件下载期间出现。

I can't understand why this problem is happening. 我不明白为什么会发生此问题。 I've also checked the logcat but found nothing that can help me to understand the root of the problem. 我也检查了logcat,但没有发现任何可以帮助我理解问题根源的东西。

Any idea? 任何想法?

Some of my code : 我的一些代码:

    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());
                }
            });

基于问题,库在内存使用方面存在严重问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM