使用Glide加载的图片保存到相册,保存gif到相册
2018-02-28 09:01
549 查看
项目中保存图片到相册这个功能很简单,但是如果有gif就相对难一些,需要判断是否是gif,本文将该功能做相应记录
String imagePath=null;
Drawable drawable = mImageView.getDrawable();
String imagePath=null;
Drawable drawable = mImageView.getDrawable();
//判断图片类型 if (drawable instanceof GifDrawable){ imagePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+System.currentTimeMillis()+".gif"; }else { imagePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+System.currentTimeMillis()+".png"; } /** * 拷贝到指定路径 保存图片和动态图 */ final String finalImagePath = imagePath; new Thread(new Runnable() { @Override public void run() { //java.lang.IllegalArgumentException: YOu must call this method on a background thread //必须在子线程中进行 String path = getImagePath(mImageUrl); copyFile(path, finalImagePath); System.out.println("保存地址;"+ finalImagePath); Intent intentBroadcast = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); File file = new File(finalImagePath); intentBroadcast.setData(Uri.fromFile(file)); activity.sendBroadcast(intentBroadcast); } }).start();
/** * Glide 获得图片缓存路径 */ private String getImagePath(String imgUrl) { String path = null; FutureTarget<File> future = Glide.with(this) .load(imgUrl) .downloadOnly(Target.SIZE_ORIGINAL,Target.SIZE_ORIGINAL); try { File cacheFile = future.get(); path = cacheFile.getAbsolutePath(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } return path; } public void copyFile(String oldPath, final String newPath) { try { int bytesum = 0; int byteread = 0; File oldfile = new File(oldPath); if (oldfile.exists()) { //文件存在时 InputStream inStream = new FileInputStream(oldPath); //读入原文件 FileOutputStream fs = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; int length; while ( (byteread = inStream.read(buffer)) != -1) { bytesum += byteread; //字节数 文件大小 System.out.println(bytesum); fs.write(buffer, 0, byteread); } inStream.close(); } new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { ToastUtils.showShortToast(activity,"以保存至"+newPath); } }); } catch (Exception e) { e.printStackTrace(); } }
别忘了添加权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
相关文章推荐
- android Glide图片加载库的使用 支持GIF的图片播放
- 使用Glide加载、缓存图片、Gif、解决背景出现浅绿色、GlideModules冲突
- Glide加载图片并保存到本地相册
- 选着图片后可以编辑图片和如何使用摄像机获取图片并保存相册
- 在.net中使用GDI+来提高gif图片的保存画质
- 本地保存gif图片,实现第二次不网络加载
- 新手使用长按手势将网络图片保存至系统相册出现的问题
- Google推荐的图片加载库Glide介绍及使用
- android glide 图片加载框架使用
- 使用GDI+提高gif图片的保存画质
- 在.net中使用GDI来提高gif图片的保存画质
- Android使用Glide加载Gif.解决Glide加载Gif非常慢问题
- Yelp app是如何使用Glide优化图片加载的
- Android 平滑图片加载和缓存库 Glide 使用详解
- github图片加载框架glide使用介绍
- Fresco 使用笔记(一):加载gif图片并播放
- iOS-使用系统类库加载gif格式图片
- Android开发:使用Glide动态加载圆形图片和圆角图片--图片开发框架
- [译]在.net中使用GDI+来提高gif图片的保存画质
- Android开发:使用Glide动态加载圆形图片和圆角图片