使用Glide原图下载方法
2017-05-10 10:28
92 查看
步骤:
1、先查看Glide版本,然后依赖,地址2、接打开Glide的wiki文档,查看自定义目标类,地址
3、贴代码:
权限:
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>操作:
/** * 下载图片 * @param iconUrl */ private void downloadIMG(String iconUrl) { int myWidth = Target.SIZE_ORIGINAL; int myHeight = Target.SIZE_ORIGINAL; Glide.with(context) .load(iconUrl) .asBitmap() .into(new SimpleTarget<Bitmap>(myWidth, myHeight) { @Override public void onResourceReady(Bitmap bitmap, GlideAnimation anim) { if(null!=bitmap) saveIMG(bitmap); } }); } /** * 保存图片到本地 * @param bitmap */ private void saveIMG(Bitmap bitmap) { //可访问的图片文件夹 File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile(); //在图片文件夹下新建自己的文件夹,保存图片 String dirName = "MyPicture"; File appDir = new File(file ,dirName); if (!appDir.exists()) { appDir.mkdirs(); } //命名图片并保存 String picName = System.currentTimeMillis() + ".jpg"; File currentFile = new File(appDir, picName); FileOutputStream fos = null; try { fos = new FileOutputStream(currentFile); bitmap.compress(Bitmap.CompressFormat.JPEG,100 , fos);//质量为100表示设置压缩率为0 fos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } // 通知图库更新 context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(currentFile.getPath())))); }
注意:
测试机小米5s,,默认没有弹出存储权限,需要手动进入设置--》授权管理--》应用权限管理--》找到你的APP--》点击读写手机存储,选中允许
note:当前查看Glide时间是2017年5月10号,再配置前请查看最新版本!
参考
相关文章推荐
- Mozilla Firefox中使用Flashget等下载工具的方法
- cssQuery()的下载与使用方法
- 【用Python写爬虫】获取html的方法【四】:使用urllib下载文件
- 上传下载组件SmartUpload使用方法
- 使用TortoiseSVN下载sourceforge.net上ogreode源代码的方法
- VB.NET中使用FTP下载文件的两种方法。
- instsrv.exe下载和使用方法(添加删除系统服务工具)
- VB.NET中使用FTP下载文件的两种方法
- (四)线程--使用线程回调方法(示例下载)
- 简单实用的分页控件 AspNetPager.dll 下载地址、使用方法、含有代码示例
- cssQuery()的下载与使用方法
- Data Access Application Block(Enterprise Library 3.1)的下载以及使用方法介绍
- WordPress 插件――CoolCode使用方法与下载
- vs2008中文正式版使用安装方法及下载
- Enjoy! 多达400多万的动态搞笑,爱情,友情。。。。表情下载 表情表情自定义表情表情图片下载 自定义QQ表情使用方法完全图解
- IYI referer插件下载与使用方法和增加Referer功能
- VB.NET中使用FTP下载文件的两种方法
- Visual Studio 2008中文正式版使用安装方法及下载
- Xenocode 2006零售版破解版下载及使用方法
- freetextbox 使用方法+下载