日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android Retrofit下载图片

發布時間:2023/12/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android Retrofit下载图片 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?Retrofit通過Url下載圖片

1.首先工具類:

/** * ClassName:DownloadImageUtils * Description TODO 下載圖片 * created by 漠天 * Data 2016/12/20 11:35 */ public class DownloadImageUtils {/** * 下載圖片到SD卡 * @param mApi * @param url * @param imageName */ public static void downloadLatestFeature(AppServiceApi mApi, final String url, final String imageName){Call<ResponseBody> resultCall = AppService.downloadLatestFeature(mApi,url); resultCall.enqueue(new Callback<ResponseBody>() {@Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {writeResponseBodyToDisk(imageName,response.body()); }@Override public void onFailure(Call<ResponseBody> call, Throwable t) {}}); }/** * 保存下載的圖片流寫入SD卡文件 * @param imageName xxx.jpg * @param body image stream */ public static void writeResponseBodyToDisk(String imageName, ResponseBody body) {if(body==null){ToastUtils.showToast("圖片源錯誤"); return; }try {InputStream is = body.byteStream(); File fileDr = new File(APP_IMAGE_DIR); if (!fileDr.exists()) {fileDr.mkdir(); }File file = new File(APP_IMAGE_DIR, imageName); if (file.exists()) {file.delete(); file = new File(APP_IMAGE_DIR, imageName ); }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(); }} }

2.接口方法:

/** * 下載最新模板圖片 * @param api */ public static Call<ResponseBody> downloadLatestFeature(AppServiceApi api, String imageUrl) {return api.downloadLatestFeature(imageUrl); }


3.Retrofit接口調用:(主要就是這部分,@Streaming 和 Call<ResponseBody>?

/** * 下載最新模板 * * @return */ @Streaming @GET Call<ResponseBody> downloadLatestFeature(@Url String fileUrl);



from :  http://blog.csdn.net/newsolider2012/article/details/54644762




我的demo DownloadImage





總結

以上是生活随笔為你收集整理的android Retrofit下载图片的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。