使用Retrofit和Rxjava下载启动图图片 - Go语言中文社区

使用Retrofit和Rxjava下载启动图图片


原理

1.通过网络请求得到server上的图片路径

2.通过Retrofit和rxjava构建下载接口

3.通过数据流把图片保存到手机中

4.当下次打开客户端时,读取保存到手机中的图片


大致的流程就这么多,下面贴一些主要的代码

1.通过Retrofit构建下载接口

private  Retrofit retrofit;
private HttpServiceInterface httpServiceInterface;

public ImgDownManager(String url){
    retrofit = new Retrofit.Builder()
            .baseUrl(url+"/")
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build();
    httpServiceInterface = retrofit.create(HttpServiceInterface.class);

}
public Observable<ResponseBody> downPic(String url){
    return httpServiceInterface.downPic(url);
}

@Streaming一定要加上,防止oom,返回的参数一定是ResponeBody
@Streaming
@GET
Observable<ResponseBody> downPic(@Url String fileUrl );


2,保存数据到对应的目录下

    public void writeResponseBodyToDisk(Context context,String url, ResponseBody body) {
        try {
            InputStream is = body.byteStream();
//            File file = new File(path, "download.jpg");
            String suffix = url.substring(url.lastIndexOf("."));
            String path =context.getFilesDir()+ File.separator+"loadingImg";
            File fileDr = new File(path);
            if(!fileDr.exists()){
                fileDr.mkdir();
            }
            File file = new File(path,"loadingImg"+suffix);
            if(file.exists()){
                file.delete();
                file= new File(path,"loadingImg"+suffix);
            }
            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();
        }
    }

3.剩下的代码就是根据路径读取图片了,这里使用了线程池进行图片的读取,使用Handler进行读取成功的通知

ThreadPoolManager.newInstance().addExecuteTask(decodeImage);

private Runnable decodeImage = new Runnable() {

    @Override
    public void run() {
        Log.i("pengsong", "decodeFile");
        try {
            if (mLoadingBitmap == null || mLoadingBitmap.isRecycled()) {
                mLoadingBitmap = BitmapFactory.decodeFile(imgPath);
            }
            mHandler.sendEmptyMessage(SET_BITMAP);
        } catch (Exception e) {
            Log.i("pengsong", "decodeFile", e);
        } catch (OutOfMemoryError error) {
            if (mLoadingBitmap != null && !mLoadingBitmap.isRecycled()) {
                mLoadingBitmap.recycle();
                mLoadingBitmap = null;
            }
            System.gc();
        }
    }
};
private Handler mHandler = new Handler() {
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case SET_BITMAP:
                Log.i("pengsong", "Handle SET_BITMAP message");
                if (isFinishing()) {
                    return;
                }
                if (mLoadingBitmap != null) {
                    imageView.setImageBitmap(mLoadingBitmap);
                }
                break;
        }
    }
};


版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/Newsolider2012/article/details/54644762
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 22:39:49
  • 阅读 ( 898 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢