【发布时间】: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