赞
踩
- /**
- * Created by vonchenchen on 2015/12/11 0011.
- */
- public class ImageLoaderManager {
-
- //ImageView 的文件缓存
- public final static String FILE_IMAGELOADER_CACHE = Environment
- .getExternalStorageDirectory().getAbsolutePath()
- + "/vonchenchen/ImageLoaderCache/";
-
- //保存裁剪后照片的路径
- public final static String FILE_SAVEPATH = Environment
- .getExternalStorageDirectory().getAbsolutePath()
- + "/vonchenchen/Portrait/";
-
- //保存直接拍照后未经剪裁照片的路径
- public final static String FILE_SAVEPATH_CAMERA = Environment
- .getExternalStorageDirectory().getAbsolutePath()
- + "/vonchenchen/Camera/";
-
- //保存经过模糊处理后的图片路径
- public final static String FILE_SAVEPATH_FUZZY = Environment
- .getExternalStorageDirectory().getAbsolutePath()
- + "/vonchenchen/Fuzzy/";
-
- //使用ImageLoader下载后需要上传的图片,单独拷贝到这个目录下,并将文件名修改为相应的格式
- public final static String FILE_SAVEPATH_UPLOAD = Environment
- .getExternalStorageDirectory().getAbsolutePath()
- + "/vonchenchen/Upload/";
-
- //裁剪后照片的前缀名 文件名:FILE_SAVENAME + timeStamp + ".jpg"
- public final static String FILE_SAVENAME_CROP = "vonchenchen";
- //直接拍照后照片的前缀名 文件名:FILE_SAVENAME_CAMERA + timeStamp + ".jpg"/other
- public final static String FILE_SAVENAME_CAMERA = "vonchenchen";
- }
- //解压图片
- protraitBitmap = ImageUtils.convertToBitmap(protraitPath, 200, 200);
- //将位图进行处理
- protraitBitmap = BlurUtil.blurBitmap(protraitBitmap, MainActivity.this);
- //将处理结果压缩为jpeg,写入指定文件
- protraitUri = saveCroppedImage(protraitBitmap, "test");
-
- //用ImageLoader加载这个文件 此处没有知道option,默认不使用内存缓存,可以根据实际情况进行调整
- ImageLoader.getInstance().displayImage(protraitUri.toString(), mImageView, new ImageLoadingListener() {
- @Override
- public void onLoadingStarted(String s, View view) {
- }
- @Override
- public void onLoadingFailed(String s, View view, FailReason failReason) {
- }
- @Override
- public void onLoadingComplete(String s, View view, Bitmap bitmap) {
- //clearDocument(FILE_SAVEPATH_FUZZY);
- }
- @Override
- public void onLoadingCancelled(String s, View view) {
- }
- });
- }
- //获取图片在硬盘缓存上的路径
- String path = getImageLoaderDiskCacheImageName(uriStr, new HashCodeFileNameGenerator());
- System.out.print(path);
-
- //将图片从ImageView维护的硬盘缓存中取出,拷贝到我们指定的路径下
- File file = copyToUpLoadFileAndRename(path, "upload.jpg");
- //获取当前图片,这种常用于下载后上传,需要知道图片路径
- String uploadPath = file.getAbsolutePath();
- /**
- * 获取ImageLoader的硬盘缓存中对应的路径
- * @param uriStr 图片资源uri
- * @param generator 给图片设置名字的generator,需要和config的config中设置的generator一样
- * @return
- */
- private String getImageLoaderDiskCacheImageName(String uriStr, FileNameGenerator generator){
- String name = generator.generate(uriStr);
- String path = ImageLoaderManager.FILE_IMAGELOADER_CACHE + name;
- return path;
- }
- /**
- * 将图片从ImageLoader的硬盘缓存路径拷贝到上传路径,并指定新的文件名
- * 复制成功返回复制的文件
- * @param path 源路径
- * @param newName 我们指定的文件名
- */
- public File copyToUpLoadFileAndRename(String path, String newName){
- File src = new File(path);
- File des = new File(ImageLoaderManager.FILE_SAVEPATH_UPLOAD + newName);
-
- String savePath = "";
- String storageState = Environment.getExternalStorageState();
- if (storageState.equals(Environment.MEDIA_MOUNTED)) {
- savePath = ImageLoaderManager.FILE_SAVEPATH_UPLOAD;
- File savedir = new File(savePath);
- if (!savedir.exists()) {
- savedir.mkdirs();
- }
- }
-
- if(copyToAnotherDocument(src, des) != -1){
- return des;
- }else{
- return null;
- }
- }
- /**
- * 将一个文件拷贝到指定文件目录下,并修文件名
- * @param src
- * @param des
- */
- private int copyToAnotherDocument(File src, File des){
- //long time=new Date().getTime();
- int length=1024*10;
- try {
- FileInputStream in=new FileInputStream(src);
- FileOutputStream out=new FileOutputStream(des);
- byte[] buffer=new byte[length];
- while(true){
- int ins=in.read(buffer);
- if(ins==-1){
- in.close();
- out.flush();
- out.close();
- //return new Date().getTime()-time;
- return 1;
- }else
- out.write(buffer,0,ins);
- }
- }catch (Exception e){
- }finally {
- }
- return -1;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。