Search in sources :

Example 1 with FileCallBack

use of com.zhy.http.okhttp.callback.FileCallBack in project Mvp-Rxjava-Retrofit-dagger2 by pengMaster.

the class MediaRecordPresenter method downLoadVideo.

/**
 * 1.视频已存在 加载本地视频
 * 2.视频不存在  下载
 *
 * @param fileUrl     下载地址
 * @param name        视频名字
 * @param progressBar 进度条
 */
public void downLoadVideo(String fileUrl, String name, final ProgressBar progressBar) {
    String packagePath = FileUtils.getPackagePath(mApplication) + "/video/";
    boolean dirs = FileUtils.createDirs(packagePath);
    if (!dirs) {
        mRootView.showMessage("文件夹创建失败");
    }
    String videoPath = packagePath + name;
    File videoFile = new File(videoPath);
    if (videoFile.exists()) {
        mRootView.startVideo(videoFile);
        return;
    } else {
        progressBar.setVisibility(View.VISIBLE);
        progressBar.setMax(100);
    }
    OkHttpUtils.get().url(fileUrl).addParams("", "").build().execute(new FileCallBack(packagePath, name) {

        @Override
        public void inProgress(float progress) {
            int i = (int) (progress * 100);
            progressBar.setProgress(i);
        }

        @Override
        public void onError(Request request, Exception e) {
            progressBar.setVisibility(View.GONE);
            e.printStackTrace();
        }

        @Override
        public void onResponse(File response) {
            progressBar.setVisibility(View.GONE);
            mRootView.startVideo(response);
        }
    });
}
Also used : FileCallBack(com.zhy.http.okhttp.callback.FileCallBack) Request(com.squareup.okhttp.Request) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

Aggregations

Request (com.squareup.okhttp.Request)1 FileCallBack (com.zhy.http.okhttp.callback.FileCallBack)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1