<>强业务需求强>
在素材列表页面的九宫格素材中,展示网络请求加载的图片。如果用户点击保存按钮,则保存若干张图片到本地。具体做法是,使用滑翔加载图片,然后设置侦听器监听,在图片请求成功onResourceReady后,将图片资源资源保存到集合中。这个时候,如果点击保存控件,则循环遍历图片资源集合保存到本地文件夹。
<强>具体做法代码展示强>
这个时候直接将请求网络的图片转化成位图,然后存储到集合中,然后当点击保存按钮的时候,将会保存该组集合中的多张图片到本地文件夹中。
//位图图片集合 私人ArrayList,bitmapArrayList=new ArrayList<的在(); RequestOptions RequestOptions=new RequestOptions () .transform(新GlideRoundTransform (mContext,半径,cornerType)); GlideApp.with (mIvImg.getContext ()) .asBitmap () .load (url) .listener(新的RequestListener () { @Override 公共布尔>/* * *请求网络图片 * @param url网址 */私有静态长时间=0; 公共静态InputStream HttpImage(字符串url) { 长l1=System.currentTimeMillis (); URL myFileUrl=零; 位图的位图=零; HttpURLConnection康涅狄格州=零; InputStream是=零; 尝试{ myFileUrl=新网址(URL); }捕捉(MalformedURLException e) { e.printStackTrace (); } 尝试{ 康涅狄格州=(HttpURLConnection) myFileUrl.openConnection (); conn.setConnectTimeout (10000); conn.setReadTimeout (5000); conn.setDoInput(真正的); conn.connect (); 是=conn.getInputStream (); }捕捉(IOException e) { e.printStackTrace (); 最后}{ 尝试{ 如果(!=null) { is.close (); conn.disconnect (); } }捕捉(IOException e) { e.printStackTrace (); } 长l2=System.currentTimeMillis (); 时间=(l2-l1) +时间; LogUtils.e(“毫秒值" +时间);//保存 } 返回; } ' ' '
保存到本地
InputStream InputStream=HttpImage ( “https://cache.yisu.com/upload/information/20200623/125/118438.jpg”); 字符串localImgSavePath=FileSaveUtils.getLocalImgSavePath (); 文件imageFile=新文件(localImgSavePath); 如果(! imageFile.exists ()) { .mkdirs imageFile.getParentFile () (); 尝试{ imageFile.createNewFile (); }捕捉(IOException e) { e.printStackTrace (); } } FileOutputStream安全系数=零; BufferedInputStream bis=零; 尝试{ 安全系数=new FileOutputStream (imageFile); bis=new BufferedInputStream (inputStream); byte[]新字节缓冲区=[1024]; int len; 在((len=bis.read(缓冲)!=1){ 安全系数。写(缓冲区,0,len); } }捕捉(异常e) { e.printStackTrace (); 最后}{ 尝试{ 如果(bis !=null) { bis.close (); } 如果(安全系数!=null) { fos.close (); } }捕捉(IOException e) { e.printStackTrace (); } }
<强>滑移下载图片强>
文件文件=Glide.with (ReflexActivity.this) .load (url.get (0)) .downloadOnly (500、500) . get (),
保存到本地
字符串localImgSavePath=FileSaveUtils.getLocalImgSavePath (); 文件imageFile=新文件(localImgSavePath); 如果(! imageFile.exists ()) { .mkdirs imageFile.getParentFile () (); imageFile.createNewFile (); } (文件,imageFile)复印件;/* * * * @param源输入文件 * @param目标输出文件 */公共静态空副本(目标文件源文件){ FileInputStream FileInputStream=零; FileOutputStream FileOutputStream=零; 尝试{ fileInputStream=new fileInputStream(源); fileOutputStream=new fileOutputStream(目标); byte[]新字节缓冲区=[1024]; 而(fileInputStream.read(缓冲)比;0){ fileOutputStream.write(缓冲); } }捕捉(异常e) { e.printStackTrace (); 最后}{ 尝试{ 如果(fileInputStream !=null) { fileInputStream.close (); } 如果(fileOutputStream !=null) { fileOutputStream.close (); } }捕捉(IOException e) { e.printStackTrace (); } } } ' ' '