不废话,直接上代码

[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相关推荐

  1. 使用java concurrent处理异步加载图片功能

    转载:http://marshal.easymorse.com/archives/3081 java5开始,增加了concurrent api,用于并发处理.比如起多个线程并发从网络上下载图片,然后在 ...

  2. wemall app商城源码中基于JAVA的Android异步加载图片管理器代码

    wemall doraemon是Android客户端程序,服务端采用wemall微信商城,不对原商城做任何修改,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可随意定制修改.本文分享其中 ...

  3. Bitmap如何高效加载图片

    如何高效加载Bitmap? 核心思想:利用BitmapFactory.Obtions来加载所需尺寸的图片. BitmapFactory.Obtions主要用到它的inSampleSize参数(采样率) ...

  4. java图片异步加载_使用java concurrent处理异步加载图片功能

    转载:http://marshal.easymorse.com/archives/3081 java5开始,增加了concurrent api,用于并发处理.比如起多个线程并发从网络上下载图片,然后在 ...

  5. java 多线程数据异步加载图片_JAVA多线程超时加载当网页图片

    先上图: 这一次没有采取正则匹配,而采取了最简单的java分割和替代方法进行筛选图片 它能够筛选如下的图片并保存到指定的文件夹 如: "http://xxxx/xxxx/xxx.jpg&qu ...

  6. android异步加载图片并缓存到内存和sd卡上,Android批量图片加载经典系列——采用二级缓存、异步加载网络图片...

    http://www.cnblogs.com/jerehedu/p/4560119.html 2015-06-08 09:20 by 杰瑞教育, 232 阅读, 1 评论, 收藏, 编辑一.问题描述 ...

  7. Android开发之ListView异步加载图片

    ListView这个控件对于大家肯定不会陌生,即使你是初学者相信也会用ListView.因为ListView这个控件实在是太常用,可以说基本上每一个项目开发都会用到它,今天这篇博客主要讲解,ListV ...

  8. 第十六章——保存、加载数据和应用程序状态

    在 iOS 应用程序中有许多方法来保存和加载数据. 本章将介绍一些最常见的机制,以及您在iOS中写入或读取文件系统所需的概念. 接下来,您将更新 Homepwner,使其数据在运行之间保持不变(图16 ...

  9. tensor和模型 保存与加载 PyTorch

    PyTorch教程-7:PyTorch中保存与加载tensor和模型详解 保存和读取Tensor PyTorch中的tensor可以保存成 .pt 或者 .pth 格式的文件,使用torch.save ...

最新文章

  1. Centos用yum安装X Winodw
  2. java下拉列表选日期_iPhone应用程序:日期选择器查看下拉列表
  3. DataSnap 用TStream 传递大数据 返回流大小为-1的情况
  4. application.properties中自定义属性的使用
  5. 企业级分布式事务设计实践解决方案
  6. COSCon'21 参会指南 你想要的这里都有
  7. LeetCode刷题——字符串转换整数
  8. oel6mysql_OEL6.5_X86平台部署Oracle 11gR2 RAC并配置ACFS
  9. IDEA导入项目出现红色J问题解决
  10. 微信小程序:border属性
  11. 读取金山词霸的词库程序
  12. 第五届上海市大学生网络安全大赛
  13. 接力贷合力贷你知道吗?
  14. windows操作系统序列号大全(经典之作)
  15. kernel下HDMI调试记录
  16. MyBatis框架学习笔记01:初生牛犊
  17. 【FTP】一、什么是FTP?
  18. 【西门子案例】西门子1200PLC 传送带控制升级版
  19. 全球名校AI课程库(43)| 李宏毅 · 机器学习(深度学习)课程『Machine Learning』
  20. 地产物业短信模板大全

热门文章

  1. 数据库高并发的解决方案
  2. linux驱动(一):linux驱动框架
  3. 编写sdk提供给第三方使用(比如接口请求类)
  4. 对random.seed()函数的理解
  5. C++ 并发编程(从C++11到C++17)
  6. maven 3.6.1版本下载地址
  7. 学习web渗透测试国内、国外在线网站
  8. thinkphp 打开速度缓慢,大多由于数据库读取问题!解决方法
  9. 我对refactoring的思考
  10. L1-022 奇偶分家(c)