转自:http://blog.csdn.net/picasso_l/article/details/50679177
最近做项目,要下载图片到本地(不是指缓存),之前用过HttpURLConnection和HttpCLient,但是感觉太繁琐,而且6.0之后到Apache包也不支持了,后来我想到了Picasso的缓存,Picasso缓存图片,那肯定是下载到本地实现了,我们只要更改下本地路径,就能轻松实现了。
废话不多说,直接上代码了。
private void download() {
//获得图片的地址
String url = mList.get(mPosition);
//Target
Target target = new Target(){
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
String imageName = System.currentTimeMillis() + ".png";
File dcimFile = FileUtil
.getDCIMFile(FileUtil.PATH_PHOTOGRAPH,imageName);
LogCat.i("bitmap="+bitmap);
FileOutputStream ostream = null;
try {
ostream = new FileOutputStream(dcimFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(PicActivity.this,"图片下载至:"+dcimFile,Toast.LENGTH_SHORT).show();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
//Picasso下载
Picasso.with(this).load(url).into(target);
}
应网友要求,贴出FileUtil代码:
public class FileUtil {
/**
* 拍照路径
*/
private static String FILE_NAME = "userIcon.jpg";
public static String PATH_PHOTOGRAPH = "/LXT/";
private static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state);
}
public static File getAvailableCacheDir() {
if (isExternalStorageWritable()) {
return App.app.getExternalCacheDir();
} else {
return App.app.getCacheDir();
}
}
public static String getAvatarCropPath() {
return new File(getAvailableCacheDir(), FILE_NAME).getAbsolutePath();
}
public static String getPublishPath(String fileName) {
return new File(getAvailableCacheDir(), fileName).getAbsolutePath();
}
/**
* 保存图片
*
* @param bitmap
* @param filePath
*/
public static void saveBitmap(Bitmap bitmap, String filePath) {
FileOutputStream bos = null;
File file = new File(filePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
bos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void deleteDir(File directory) {
if (directory != null){
if (directory.isFile()) {
directory.delete();
return;
}
if (directory.isDirectory()) {
File[] childFiles = directory.listFiles();
if (childFiles == null || childFiles.length == 0) {
directory.delete();
return;
}
for (int i = 0; i < childFiles.length; i++) {
deleteDir(childFiles[i]);
}
directory.delete();
}
}
}
public static File getDCIMFile(String filePath, String imageName) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) { // 文件可用
File dirs = new File(Environment.getExternalStorageDirectory(),
"DCIM"+filePath);
if (!dirs.exists())
dirs.mkdirs();
File file = new File(Environment.getExternalStorageDirectory(),
"DCIM"+filePath+imageName);
if (!file.exists()) {
try {
//在指定的文件夹中创建文件
file.createNewFile();
} catch (Exception e) {
}
}
return file;
} else {
return null;
}
}
public static File saveBitmap2(Bitmap bitmap, String fileName, File baseFile) {
FileOutputStream bos = null;
File imgFile = new File(baseFile, "/" + fileName);
try {
bos = new FileOutputStream(imgFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return imgFile;
}
public static File getBaseFile(String filePath) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) { // 文件可用
File f = new File(Environment.getExternalStorageDirectory(),
filePath);
if (!f.exists())
f.mkdirs();
return f;
} else {
return null;
}
}
public static String getFileName() {
String fileName = FILE_NAME ;
return fileName;
}
/**
* 由指定的路径和文件名创建文件
*/
public static File createFile(String path, String name) throws IOException {
File folder = new File(path);
if (!folder.exists()) {
folder.mkdirs();
}
File file = new File(path + name);
if (!file.exists()) {
file.createNewFile();
}
return file;
}
}
这样就轻松实现了,值得注意的是,我们经常用的是into(ImageView target),其实into还能传入很多不同的对象。今天有点晚了,先写到这,关于Picasso,还是有很多技巧可以探索的。之前还写过一篇Android 使用Picasso加载网络图片等比例缩放的文章,也欢迎大家有空去指点一下。
参考文献:
http://stackoverflow.com/questions/27729976/download-and-save-images-using-picasso