Glide加载图片并保存到本地相册

不废话,直接上代码

[java]view plaincopy

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()))));

}

}

[java]view plaincopy

publicinterfaceImageDownLoadCallBack {

voidonDownLoadSuccess(File file);

voidonDownLoadSuccess(Bitmap bitmap);

voidonDownLoadFailed();

}

[java]view plaincopy

/**

* 启动图片下载线程

*/

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();

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容