知道图片的url,然后通过Picasso进行图片加载
发送广播通知系统图库更新相册
Picasso.with(DiaActivity.this).load(url).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
**// 创建目录**
File appDir = new File(Environment.getExternalStorageDirectory(), "zhhzFeng");
if (!appDir.exists()) {
appDir.mkdir();
}
String imageType = getImageType(url); //获取图片类型
String fileName = System.currentTimeMillis() + "." + imageType;
File file = new File(appDir, fileName);
**//保存图片**
try {
FileOutputStream fos = new FileOutputStream(file);
if(TextUtils.equals(imageType,"jpg")) imageType = "jpeg";
imageType = imageType.toUpperCase();
bitmap.compress(Bitmap.CompressFormat.valueOf(imageType), 100, fos);
fos.flush();
fos.close();
Toast.makeText(DiaActivity.this,"保存成功",Toast.LENGTH_SHORT).show(); //Toast
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
**// 其次把文件插入到系统图库**
try {
MediaStore.Images.Media.insertImage(DiaActivity.this.getContentResolver(), file.getAbsolutePath(), fileName, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
**// 最后通知图库更新**
DiaActivity.this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getPath())));
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});