Glide加載圖片并保存到本地相冊(cè)

不廢話,直接上代碼

[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){

//?在這里執(zhí)行圖片保存方法

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();//注意小米手機(jī)必須這樣獲得public絕對(duì)路徑

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

}

}

//?其次把文件插入到系統(tǒng)圖庫(kù)

//????????try?{

//????????????MediaStore.Images.Media.insertImage(context.getContentResolver(),

//????????????????????currentFile.getAbsolutePath(),?fileName,?null);

//????????}?catch?(FileNotFoundException?e)?{

//????????????e.printStackTrace();

//????????}

//?最后通知圖庫(kù)更新

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

/**

*?啟動(dòng)圖片下載線程

*/

privatevoidonDownLoad(String?url)?{

DownLoadImageService?service?=newDownLoadImageService(getApplicationContext(),

url,

newImageDownLoadCallBack()?{

@Override

publicvoidonDownLoadSuccess(File?file)?{

}

@Override

publicvoidonDownLoadSuccess(Bitmap?bitmap)?{

//?在這里執(zhí)行圖片保存方法

Message?message?=newMessage();

message.what?=?MSG_VISIBLE;

handler.sendMessageDelayed(message,?delayTime);

}

@Override

publicvoidonDownLoadFailed()?{

//?圖片保存失敗

Message?message?=newMessage();

message.what?=?MSG_ERROR;

handler.sendMessageDelayed(message,?delayTime);

}

});

//啟動(dòng)圖片下載線程

newThread(service).start();

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容