volley框架的文件下载真的是很少见的了,自己做一下记录

import android.os.Environment;import android.support.annotation.NonNull;import android.util.Log;import com.android.volley.NetworkResponse;import com.android.volley.ParseError;import com.android.volley.Request;import com.android.volley.Response;import com.android.volley.VolleyLog;import com.android.volley.toolbox.HttpHeaderParser;/** * Created by CHENQING on 2019/5/23. * 自定义的Volley请求文件网络请求 */public class FileRequest extends Request {    private final Response.Listener mListener;    private static String filename;    /**     * Decoding lock so that we don't decode more than one image at a time (to avoid OOM's)     */    private static final Object sDecodeLock = new Object();    /**     * Creates a new image request, decoding to a maximum specified width and     * height. If both width and height are zero, the image will be decoded to     * its natural size. If one of the two is nonzero, that dimension will be     * clamped and the other one will be set to preserve the image's aspect     * ratio. If both width and height are nonzero, the image will be decoded to     * be fit in the rectangle of dimensions width x height while keeping its     * aspect ratio.     *     * @param url           URL of the image     * @param listener      Listener to receive the decoded bitmap     * @param errorListener Error listener, or null to ignore errors     */    public FileRequest(String url, Response.Listener listener, Response.ErrorListener errorListener) {        super(Method.GET, url, errorListener);        mListener = listener;    }    public FileRequest( int method ,String url, Response.Listener listener, Response.ErrorListener errorListener) {        super(method, url, errorListener);        mListener = listener;    }    @Override    protected Response parseNetworkResponse(NetworkResponse response) {        // Serialize all decode on a global lock to reduce concurrent heap usage.        synchronized (sDecodeLock) {            try {                if (response.data == null) {                    return Response.error(new ParseError(response));                } else {                    Log.i("getdownloadresponse" , response.headers.get("Content-Disposition"));                    filename = response.headers.get("Content-Disposition");                    return Response.success(response.data, HttpHeaderParser.parseCacheHeaders(response));                }            } catch (OutOfMemoryError e) {                VolleyLog.e("Caught OOM for %d byte image, url=%s", response.data.length, getUrl());                return Response.error(new ParseError(e));            }        }    }    //获取文件名称并传给页面    public static String getfilename(){        return filename;    }    @Override    protected void deliverResponse(byte[] response) {        mListener.onResponse(response);    }}

更多相关文章

  1. android 对文件的操作模式
  2. Android从网上下载文件
  3. Android遍历某个文件夹的图片并实现滑动查看的的Gallery
  4. Android SDcard目录文件操作
  5. android sdcard文件存储 + 媒体库更新方法
  6. 录音及播放音频文件

随机推荐

  1. 超赞,20个炫酷的数据可视化大屏(含源码)
  2. 安利 5 个拍案叫绝的 Matplotlib 骚操作!
  3. C语言学习心得(七)
  4. pandas100个骚操作:JSON自动解析为DataFra
  5. 这个可视化分析库,让你轻松玩转数据科学!
  6. k8s交付服务总结
  7. Android(安卓)蓝牙开发:第一日
  8. TFTP不能传输大于32MB的文件?
  9. 年轻人不讲武德,竟用Python让马老师表演闪
  10. 赛博朋克这么火,如果与Python结合,能有多酷