代码拉取完成,页面将自动刷新
本项目是基于开源项目 ProgressManager 进行ohos化的移植和开发的,可以通过项目标签以及github地址(https://github.com/JessYanCoding/ProgressManager )追踪到原项目版本
方法一:
编译ProgressManager的har包progress-release.har。
启动 DevEco Studio,将编译的har包,导入工程目录“entry->libs”下。
在moudle级别下的build.gradle文件中添加依赖,在dependences标签中增加对libs目录下har包的引用。
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar', '*.har'])
……
}
在导入的har包上点击右键,选择“Add as Library”对包进行引用,选择需要引用的模块,并点击“OK”即引用成功。
方法二:
repositories {
maven {
url 'http://106.15.92.248:8081/repository/Releases/'
}
}
dependencies {
implementation 'me.jessyan.progressmanager.ohos:progress:1.0.0'
}
(一)下载代码
(1)初始化,设置监听
//Okhttp/Retofit 下载监听
ProgressManager.getInstance().addResponseListener(mDownloadUrl, getDownloadListener());
(2)下载
/**
* 点击开始下载资源,为了演示,就不做重复点击的处理,即允许用户在还有进度没完成的情况下,使用同一个 url 开始新的下载
*/
private void downloadStart() {
new Thread(() -> {
try {
Request request = new Request.Builder()
.url(mDownloadUrl)
.build();
Response response = mOkHttpClient.newCall(request).execute();
InputStream is = response.body().byteStream();
//为了方便就不动态申请权限了,直接将文件放到CacheDir()中
File file = new File(getCacheDir(), "download");
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buffer = new byte[1024];
int len;
while ((len = bis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.flush();
fos.close();
bis.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
//当外部发生错误时,使用此方法可以通知所有监听器的 onError 方法
ProgressManager.getInstance().notifyOnErorr(mDownloadUrl, e);
}
}).start();
}
(3)下载进度变化监听
private ProgressListener getDownloadListener() {
return new ProgressListener() {
@Override
public void onProgress(ProgressInfo progressInfo) {
// 如果你不屏蔽用户重复点击上传或下载按钮,就可能存在同一个 Url 地址,上一次的上传或下载操作都还没结束,
// 又开始了新的上传或下载操作,那现在就需要用到 id(请求开始时的时间) 来区分正在执行的进度信息
// 这里我就取最新的下载进度用来展示,顺便展示下 id 的用法
if (mLastDownloadingInfo == null) {
mLastDownloadingInfo = progressInfo;
}
//因为是以请求开始时的时间作为 Id ,所以值越大,说明该请求越新
if (progressInfo.getId() < mLastDownloadingInfo.getId()) {
return;
} else if (progressInfo.getId() > mLastDownloadingInfo.getId()) {
mLastDownloadingInfo = progressInfo;
}
int progress = mLastDownloadingInfo.getPercent();
mDownloadProgress.setProgressValue(progress);
mDownloadProgressText.setText(progress + "%");
LogUtils.log(LogUtils.DEBUG,TAG, "--Download-- " + progress + " % " + mLastDownloadingInfo.getSpeed() + " byte/s " + mLastDownloadingInfo.toString());
if (mLastDownloadingInfo.isFinish()) {
//说明已经下载完成
LogUtils.log(LogUtils.DEBUG,TAG, "--Download-- finish");
}
}
@Override
public void onError(long id, Exception e) {
mHandler.postSyncTask(new Runnable() {
@Override
public void run() {
mDownloadProgress.setProgressValue(0);
mDownloadProgressText.setText("error");
}
});
}
};
}
(二)上传文件
(1)上传初始化
//Okhttp/Retofit 上传监听
ProgressManager.getInstance().addRequestListener(mUploadUrl, getUploadListener());
(2)上传
/**
* 点击开始上传资源,为了演示,就不做重复点击的处理,即允许用户在还有进度没完成的情况下,使用同一个 url 开始新的上传
*/
private void uploadStart() {
new Thread(() -> {
try {
//为了方便就不动态申请权限了,直接将文件放到CacheDir()中
File file = new File(getCacheDir(), "a.java");
//读取Assets里面的数据,作为上传源数据
writeToFile(getRawFile(MainAbilitySlice.this, "a.java"), file);
Request request = new Request.Builder()
.url(mUploadUrl)
.post(RequestBody.create(MediaType.parse("multipart/form-data"), file))
.build();
Response response = mOkHttpClient.newCall(request).execute();
response.body();
} catch (IOException e) {
e.printStackTrace();
//当外部发生错误时,使用此方法可以通知所有监听器的 onError 方法
ProgressManager.getInstance().notifyOnErorr(mUploadUrl, e);
}
}).start();
}
(3)上传监听变化
private ProgressListener getUploadListener() {
return new ProgressListener() {
@Override
public void onProgress(ProgressInfo progressInfo) {
// 如果你不屏蔽用户重复点击上传或下载按钮,就可能存在同一个 Url 地址,上一次的上传或下载操作都还没结束,
// 又开始了新的上传或下载操作,那现在就需要用到 id(请求开始时的时间) 来区分正在执行的进度信息
// 这里我就取最新的上传进度用来展示,顺便展示下 id 的用法
if (mLastUploadingingInfo == null) {
mLastUploadingingInfo = progressInfo;
}
//因为是以请求开始时的时间作为 Id ,所以值越大,说明该请求越新
if (progressInfo.getId() < mLastUploadingingInfo.getId()) {
return;
} else if (progressInfo.getId() > mLastUploadingingInfo.getId()) {
mLastUploadingingInfo = progressInfo;
}
int progress = mLastUploadingingInfo.getPercent();
mUploadProgress.setProgressValue(progress);
mUploadProgressText.setText(progress + "%");
LogUtils.log(LogUtils.DEBUG, TAG, "--Upload-- " + progress + " % " + mLastUploadingingInfo.getSpeed() + " byte/s " + mLastUploadingingInfo.toString());
if (mLastUploadingingInfo.isFinish()) {
//说明已经上传完成
LogUtils.log(LogUtils.DEBUG, TAG, "--Upload-- finish");
}
}
@Override
public void onError(long id, Exception e) {
mHandler.postSyncTask(new Runnable() {
@Override
public void run() {
mUploadProgress.setProgressValue(0);
mUploadProgressText.setText("error");
}
});
}
};
}
(三)图片加载代码
(1)初始化,设置监听
mNewImageUrl = ProgressManager .getInstance().addDiffResponseListenerOnSameUrl("http://jessyancoding.github.io/images/RxCache.png", getGlideListener());
(2)加载设置
/**
* 点击开始 Glide 加载图片,为了演示,就不做重复点击的处理,目前这个在支持现有的ohos 的glide中,存在一些问题,无法兼容起来,
* 做了单独处理
*/
private void glideStart() {
LoadPictureUtils.with(this)
.def(ResourceTable.Media_A)
.load(mNewImageUrl)
.into(mImageView);
}
(3) 图片进度加载变化监听
private ProgressListener getGlideListener() {
return new ProgressListener() {
@Override
public void onProgress(ProgressInfo progressInfo) {
int progress = progressInfo.getPercent();
mGlideProgress.setProgressValue(progress);
mGlideProgressText.setText(progress + "%");
LogUtils.log(LogUtils.DEBUG,TAG, "--Glide-- " + progress + " % " + progressInfo.getSpeed() + " byte/s " + progressInfo.toString());
if (progressInfo.isFinish()) {
//说明已经加载完成
}
}
@Override
public void onError(long id, Exception e) {
mHandler.postSyncTask(new Runnable() {
@Override
public void run() {
mGlideProgress.setProgressValue(0);
mGlideProgressText.setText("error");
}
});
}
};
}
v1.0.0
Glide 图片加载,文件下载和上传等进度显示。
Copyright 2017, jessyan
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型