Glide加载图片并保存到本地返回file,bitmap
来源:互联网 发布:java date sethour 编辑:程序博客网 时间:2024/05/01 17:56
不废话,直接上代码
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.net.Uri;
- import com.baguanv.jinba.utils.Const;
- import com.bumptech.glide.Glide;
- import com.bumptech.glide.request.target.Target;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- /**
- * 图片下载
- *
- */
- public class DownLoadImageService implements Runnable {
- private String url;
- private Context context;
- private ImageDownLoadCallBack callBack;
- private File currentFile;
- public DownLoadImageService(Context context, String url, ImageDownLoadCallBack callBack) {
- this.url = url;
- this.callBack = callBack;
- this.context = context;
- }
- @Override
- public void run() {
- File file = null;
- Bitmap bitmap = null;
- try {
- // file = Glide.with(context)
- // .load(url)
- // .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
- // .get();
- bitmap = Glide.with(context)
- .load(url)
- .asBitmap()
- .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
- .get();
- if (bitmap != null){
- // 在这里执行图片保存方法
- saveImageToGallery(context,bitmap);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- // if (file != null) {
- // callBack.onDownLoadSuccess(file);
- // } else {
- // callBack.onDownLoadFailed();
- // }
- if (bitmap != null && currentFile.exists()) {
- callBack.onDownLoadSuccess(bitmap);
- } else {
- callBack.onDownLoadFailed();
- }
- }
- }
- public void saveImageToGallery(Context context, Bitmap bmp) {
- // 首先保存图片
- String File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();//注意小米手机必须这样获得public绝对路径
- String fileName = ”新建文件夹”;
- File appDir = new File(file ,fileName);
- if (!appDir.exists()) {
- appDir.mkdirs();
- }
- String fileName = System.currentTimeMillis() + ”.jpg”;
- currentFile = new File(appDir, fileName);
- FileOutputStream fos = null;
- try {
- fos = new FileOutputStream(currentFile);
- bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
- fos.flush();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (fos != null) {
- fos.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- // 其次把文件插入到系统图库
- // try {
- // MediaStore.Images.Media.insertImage(context.getContentResolver(),
- // currentFile.getAbsolutePath(), fileName, null);
- // } catch (FileNotFoundException e) {
- // e.printStackTrace();
- // }
- // 最后通知图库更新
- context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
- Uri.fromFile(new File(currentFile.getPath()))));
- }
- }
import android.content.Context;import android.content.Intent;import android.graphics.Bitmap;import android.net.Uri;import com.baguanv.jinba.utils.Const;import com.bumptech.glide.Glide;import com.bumptech.glide.request.target.Target;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;/** * 图片下载 * */public class DownLoadImageService implements Runnable { private String url; private Context context; private ImageDownLoadCallBack callBack; private File currentFile; public DownLoadImageService(Context context, String url, ImageDownLoadCallBack callBack) { this.url = url; this.callBack = callBack; this.context = context; } @Override public void run() { File file = null; Bitmap bitmap = null; try {// file = Glide.with(context)// .load(url)// .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)// .get(); bitmap = Glide.with(context) .load(url) .asBitmap() .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) .get(); if (bitmap != null){ // 在这里执行图片保存方法 saveImageToGallery(context,bitmap); } } catch (Exception e) { e.printStackTrace(); } finally {// if (file != null) {// callBack.onDownLoadSuccess(file);// } else {// callBack.onDownLoadFailed();// } if (bitmap != null && currentFile.exists()) { callBack.onDownLoadSuccess(bitmap); } else { callBack.onDownLoadFailed(); } } } public void saveImageToGallery(Context context, Bitmap bmp) { // 首先保存图片 String File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();//注意小米手机必须这样获得public绝对路径 String fileName = "新建文件夹"; File appDir = new File(file ,fileName); if (!appDir.exists()) { appDir.mkdirs(); } String fileName = System.currentTimeMillis() + ".jpg"; currentFile = new File(appDir, fileName); FileOutputStream fos = null; try { fos = new FileOutputStream(currentFile); bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } // 其次把文件插入到系统图库// try {// MediaStore.Images.Media.insertImage(context.getContentResolver(),// currentFile.getAbsolutePath(), fileName, null);// } catch (FileNotFoundException e) {// e.printStackTrace();// } // 最后通知图库更新 context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(currentFile.getPath())))); }}
- public interface ImageDownLoadCallBack {
- void onDownLoadSuccess(File file);
- void onDownLoadSuccess(Bitmap bitmap);
- void onDownLoadFailed();
- }
public interface ImageDownLoadCallBack { void onDownLoadSuccess(File file); void onDownLoadSuccess(Bitmap bitmap); void onDownLoadFailed();}
- /**
- * 启动图片下载线程
- */
- private void onDownLoad(String url) {
- DownLoadImageService service = new DownLoadImageService(getApplicationContext(),
- url,
- new ImageDownLoadCallBack() {
- @Override
- public void onDownLoadSuccess(File file) {
- }
- @Override
- public void onDownLoadSuccess(Bitmap bitmap) {
- // 在这里执行图片保存方法
- Message message = new Message();
- message.what = MSG_VISIBLE;
- handler.sendMessageDelayed(message, delayTime);
- }
- @Override
- public void onDownLoadFailed() {
- // 图片保存失败
- Message message = new Message();
- message.what = MSG_ERROR;
- handler.sendMessageDelayed(message, delayTime);
- }
- });
- //启动图片下载线程
- new Thread(service).start();
- }
/** * 启动图片下载线程 */ private void onDownLoad(String url) { DownLoadImageService service = new DownLoadImageService(getApplicationContext(), url, new ImageDownLoadCallBack() { @Override public void onDownLoadSuccess(File file) { } @Override public void onDownLoadSuccess(Bitmap bitmap) { // 在这里执行图片保存方法 Message message = new Message(); message.what = MSG_VISIBLE; handler.sendMessageDelayed(message, delayTime); } @Override public void onDownLoadFailed() { // 图片保存失败 Message message = new Message(); message.what = MSG_ERROR; handler.sendMessageDelayed(message, delayTime); } }); //启动图片下载线程 new Thread(service).start(); }
1 0
- Glide加载图片并保存到本地返回file,bitmap
- Glide加载图片并保存到本地返回file,bitmap
- Glide加载图片并保存到本地相册
- Android ZoomImageView手势缩放拖动图片,并将Glide加载的网络图片保存到本地。
- 保存BitMap,File到本地
- 将bitmap对象保存到本地,返回保存的图片路径
- 使用AsyncTask加载网络图片并保存到手机本地
- view转bitmap&&保存图片到本地
- Glide-加载本地图片
- Bitmap保存到本地
- Android 保存Glide展示的图片到本地
- Glide下载图片并保存到指定路径
- Android画图并保存图片到本地
- 获取网络图片,并保存到本地
- android canvas 绘制bitmap并保存到本地
- 关于Android开发保存bitmap获取图片到本地方法
- 保存Bitmap到本地文件夹
- Android保存Bitmap到本地
- QuickHit项目
- 数据结构顺序查找验证程序
- 学习python的第二十五天-老程序员的忠告
- UVA 1349 Optimal Bus Route Design
- PAT B1028
- Glide加载图片并保存到本地返回file,bitmap
- 谈谈Android 6.0运行时权限理解
- 南京邮电大学离散数学实验三偏序关系中盖住关系的求取及格论中有补格的判定
- 谈谈Android 6.0运行时权限理解
- PAT B1029
- PHP——CURL类
- PROCEDURE-12
- HTTP详解(1)-工作原理
- HDU 1014 Uniform Generator 水题