java bitmap 保存 jpg_Glide加载图片并保存到本地返回file,bitmap
不废话,直接上代码
[java] view plain
copy
print ?
importandroid.content.Context;
importandroid.content.Intent;
importandroid.graphics.Bitmap;
importandroid.net.Uri;
importcom.baguanv.jinba.utils.Const;
importcom.bumptech.glide.Glide;
importcom.bumptech.glide.request.target.Target;
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
/**
* 图片下载
*
*/
publicclassDownLoadImageServiceimplementsRunnable {
privateString url;
privateContext context;
privateImageDownLoadCallBack callBack;
privateFile currentFile;
publicDownLoadImageService(Context context, String url, ImageDownLoadCallBack callBack) {
this.url = url;
this.callBack = callBack;
this.context = context;
}
@Override
publicvoidrun() {
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();
}
}
}
publicvoidsaveImageToGallery(Context context, Bitmap bmp) {
// 首先保存图片
String File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();//注意小米手机必须这样获得public绝对路径
String fileName = ”新建文件夹”;
File appDir = newFile(file ,fileName);
if(!appDir.exists()) {
appDir.mkdirs();
}
String fileName = System.currentTimeMillis() + ”.jpg”;
currentFile = newFile(appDir, fileName);
FileOutputStream fos = null;
try{
fos = newFileOutputStream(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(newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.fromFile(newFile(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()))));
}
}
[java] view plain
copy
print ?
publicinterfaceImageDownLoadCallBack {
voidonDownLoadSuccess(File file);
voidonDownLoadSuccess(Bitmap bitmap);
voidonDownLoadFailed();
}
public interface ImageDownLoadCallBack {
void onDownLoadSuccess(File file);
void onDownLoadSuccess(Bitmap bitmap);
void onDownLoadFailed();
}
[java] view plain
copy
print ?
/**
* 启动图片下载线程
*/
privatevoidonDownLoad(String url) {
DownLoadImageService service = newDownLoadImageService(getApplicationContext(),
url,
newImageDownLoadCallBack() {
@Override
publicvoidonDownLoadSuccess(File file) {
}
@Override
publicvoidonDownLoadSuccess(Bitmap bitmap) {
// 在这里执行图片保存方法
Message message = newMessage();
message.what = MSG_VISIBLE;
handler.sendMessageDelayed(message, delayTime);
}
@Override
publicvoidonDownLoadFailed() {
// 图片保存失败
Message message = newMessage();
message.what = MSG_ERROR;
handler.sendMessageDelayed(message, delayTime);
}
});
//启动图片下载线程
newThread(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();
}
java bitmap 保存 jpg_Glide加载图片并保存到本地返回file,bitmap相关推荐
- 使用java concurrent处理异步加载图片功能
转载:http://marshal.easymorse.com/archives/3081 java5开始,增加了concurrent api,用于并发处理.比如起多个线程并发从网络上下载图片,然后在 ...
- wemall app商城源码中基于JAVA的Android异步加载图片管理器代码
wemall doraemon是Android客户端程序,服务端采用wemall微信商城,不对原商城做任何修改,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可随意定制修改.本文分享其中 ...
- Bitmap如何高效加载图片
如何高效加载Bitmap? 核心思想:利用BitmapFactory.Obtions来加载所需尺寸的图片. BitmapFactory.Obtions主要用到它的inSampleSize参数(采样率) ...
- java图片异步加载_使用java concurrent处理异步加载图片功能
转载:http://marshal.easymorse.com/archives/3081 java5开始,增加了concurrent api,用于并发处理.比如起多个线程并发从网络上下载图片,然后在 ...
- java 多线程数据异步加载图片_JAVA多线程超时加载当网页图片
先上图: 这一次没有采取正则匹配,而采取了最简单的java分割和替代方法进行筛选图片 它能够筛选如下的图片并保存到指定的文件夹 如: "http://xxxx/xxxx/xxx.jpg&qu ...
- android异步加载图片并缓存到内存和sd卡上,Android批量图片加载经典系列——采用二级缓存、异步加载网络图片...
http://www.cnblogs.com/jerehedu/p/4560119.html 2015-06-08 09:20 by 杰瑞教育, 232 阅读, 1 评论, 收藏, 编辑一.问题描述 ...
- Android开发之ListView异步加载图片
ListView这个控件对于大家肯定不会陌生,即使你是初学者相信也会用ListView.因为ListView这个控件实在是太常用,可以说基本上每一个项目开发都会用到它,今天这篇博客主要讲解,ListV ...
- 第十六章——保存、加载数据和应用程序状态
在 iOS 应用程序中有许多方法来保存和加载数据. 本章将介绍一些最常见的机制,以及您在iOS中写入或读取文件系统所需的概念. 接下来,您将更新 Homepwner,使其数据在运行之间保持不变(图16 ...
- tensor和模型 保存与加载 PyTorch
PyTorch教程-7:PyTorch中保存与加载tensor和模型详解 保存和读取Tensor PyTorch中的tensor可以保存成 .pt 或者 .pth 格式的文件,使用torch.save ...
最新文章
- Centos用yum安装X Winodw
- java下拉列表选日期_iPhone应用程序:日期选择器查看下拉列表
- DataSnap 用TStream 传递大数据 返回流大小为-1的情况
- application.properties中自定义属性的使用
- 企业级分布式事务设计实践解决方案
- COSCon'21 参会指南 你想要的这里都有
- LeetCode刷题——字符串转换整数
- oel6mysql_OEL6.5_X86平台部署Oracle 11gR2 RAC并配置ACFS
- IDEA导入项目出现红色J问题解决
- 微信小程序:border属性
- 读取金山词霸的词库程序
- 第五届上海市大学生网络安全大赛
- 接力贷合力贷你知道吗?
- windows操作系统序列号大全(经典之作)
- kernel下HDMI调试记录
- MyBatis框架学习笔记01:初生牛犊
- 【FTP】一、什么是FTP?
- 【西门子案例】西门子1200PLC 传送带控制升级版
- 全球名校AI课程库(43)| 李宏毅 · 机器学习(深度学习)课程『Machine Learning』
- 地产物业短信模板大全