接收前端传给的url :
/**
*
* @param imgPath
* @param context
*/
public void loadImg(final String imgPath, final Context context) {
T.showShort("开始下载");
new Thread(new Runnable() {
@Override
public void run() {
try {
BitmapDrawable drawable = (BitmapDrawable) Glide.with(context)
.load(imgPath)
.submit(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
.get();
Bitmap bitmap = drawable.getBitmap();
String name = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".jpg";
File path = new File(Environment.getExternalStorageDirectory() + "/haha", "images");
File output = new File(path, name);
String imagePath = output.getPath();
if (!output.getParentFile().exists()) output.getParentFile().mkdirs();
File f = new File(imagePath);
OutputStream os = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
os.close();
Uri uri = Uri.fromFile(f);
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
T.showShort("下载成功,请到相册中查看");
}
});
} catch (Exception e) {
}
}
}).start();
}
本文介绍了一种使用Glide加载图片的方法,并演示了如何将加载完成的图片保存到本地设备的过程。此过程涉及创建文件目录、压缩Bitmap以及发送广播通知系统更新相册等步骤。
1136

被折叠的 条评论
为什么被折叠?



