您的位置:首页 > 其它

使用Glide原图下载方法

2017-05-10 10:28 92 查看

步骤:

1、先查看Glide版本,然后依赖,地址
2、接打开Glide的wiki文档,查看自定义目标类,地址
3、贴代码:

权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
操作:
/**
* 下载图片
* @param iconUrl
*/
private void downloadIMG(String iconUrl) {
int myWidth = Target.SIZE_ORIGINAL;
int myHeight = Target.SIZE_ORIGINAL;
Glide.with(context)
.load(iconUrl)
.asBitmap()
.into(new SimpleTarget<Bitmap>(myWidth, myHeight) {
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
if(null!=bitmap) saveIMG(bitmap);
}
});
}

/**
* 保存图片到本地
* @param bitmap
*/
private void saveIMG(Bitmap bitmap) {
//可访问的图片文件夹
File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();
//在图片文件夹下新建自己的文件夹,保存图片
String dirName = "MyPicture";
File appDir = new File(file ,dirName);
if (!appDir.exists()) {
appDir.mkdirs();
}
//命名图片并保存
String picName = System.currentTimeMillis() + ".jpg";
File currentFile = new File(appDir, picName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(currentFile);
bitmap.compress(Bitmap.CompressFormat.JPEG,100 , fos);//质量为100表示设置压缩率为0
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 通知图库更新
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.fromFile(new File(currentFile.getPath()))));
}


注意:

测试机小米5s,,默认没有弹出存储权限,需要手动进入设置--》授权管理--》应用权限管理--》找到你的APP--》点击读写手机存储,选中允许





note:当前查看Glide时间是2017年5月10号,再配置前请查看最新版本!

参考

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: