zoukankan      html  css  js  c++  java
  • android使用OkHttp或者上传图片显示进度

    public class UploadImageBody extends RequestBody {
    
      private File mFile;
      private String mContentType;
      private ProgressListener mListener;
    
      @Override
      public MediaType contentType() {
        return MediaType.parse(mContentType);
      }
    
      @Override
      public void writeTo(BufferedSink sink) throws IOException {
        Source source = null;
        try {
          source = Okio.source(mFile);
          long total = 0;
          long read;
    
          while ((read = source.read(sink.buffer(), 8192)) != -1) {
            total += read;
            sink.flush();
            if (mListener != null) {
              mListener.onProgress(total, mFile.length());
            }
          }
        } finally {
          Util.closeQuietly(source);
        }
      }
    
      @Override
      public long contentLength() {
        return mFile.length();
      }
    
      public interface ProgressListener {
    
        void onProgress(long current, long total);
      }
    
    
      public static final class Builder {
    
        private String mFilePath;
        private ProgressListener mListener;
    
        public Builder withFilePath(String filePath) {
          this.mFilePath = filePath;
          return this;
        }
    
        public Builder withListener(ProgressListener mListener) {
          this.mListener = mListener;
          return this;
        }
    
        private String getFileName() {
          return FileUtil.getFileName(mFilePath);
        }
    
        public MultipartBody.Part buildPart() {
          UploadImageBody uploadImageBody = new UploadImageBody();
          uploadImageBody.mListener = this.mListener;
          uploadImageBody.mContentType = "application/octet-stream";
          uploadImageBody.mFile = new File(mFilePath);
          return MultipartBody.Part.createFormData("file", getFileName(), uploadImageBody);
        }
      }
    }
  • 相关阅读:
    The type new View.OnClickListener(){} must implement the inherited abstract method View.Onclicklis
    vue开发环境跨域
    浅析deep深度选择器
    模块化
    highlight-current-row无效的解决方法
    element-ui的table 在页面缩放时,出现的问题
    css变量
    节流和防抖
    promise详解
    正则表达式详解
  • 原文地址:https://www.cnblogs.com/yongfengnice/p/10650901.html
Copyright © 2011-2022 走看看