imageLoader保存图片到本地相册


先在iamgeLoader设置三级缓存         

DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
                .cacheOnDisk(true).cacheInMemory(true).build();
        ImageLoader.getInstance().loadImage(iamgeUrl,displayImageOptions, new ImageLoadingListener() {
....
...
}


名字用 HashCode 用作图片名字 , 用户多次点击保存图片就不会是多张了
String fileName = new HashCodeFileNameGenerator().generate(imageUri) + ".jpeg";


图片下载完 最主要就是保存到相册 , 而且相册的名字是APP的名字 . 好像知乎APP那样 .这样做挺方便用户去查看.

path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath()+"/"+getResources().getString(R.string.app_name);


发送广播通知相册更新了~~

Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                    Uri uri = Uri.fromFile(saveImageFile);
                    intent.setData(uri);
                    PhotoActivity.this.sendBroadcast(intent);//这个广播的目的就是更新图库,发了这个广播进入相册就可以找到你保存的图片了!,记得要传你更新的file哦


下面是完整的代码了~~

/***
     * 保存图片到SD卡
     */
    public void saveImgToSD(String iamgeUrl) {


        DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
                .cacheOnDisk(true).cacheInMemory(true).build();
        ImageLoader.getInstance().loadImage(iamgeUrl,displayImageOptions, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {

            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

                String fileName = new HashCodeFileNameGenerator().generate(imageUri) + ".jpeg";


             //   String path =StorageUtils.getOwnCacheDirectory(PhotoActivity.this, "chuizhicai/image/userSaveImage").getPath();//保存的确切位置;\
                String path;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO) {
                    path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath()+"/"+getResources().getString(R.string.app_name);
                }else {
                    path = StorageUtils.getOwnCacheDirectory(PhotoActivity.this, "chuizhicai/image/userSaveImage").getPath();//保存的确切位置  }
                }
                File saveImageFile = ImgUtil.saveFile(loadedImage, fileName, path);
                if (saveImageFile!=null){
                    ToastUtil.Show_LONG(PhotoActivity.this, String.format("%s%s/%s", "图片保存到", path, fileName));
                    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                    Uri uri = Uri.fromFile(saveImageFile);
                    intent.setData(uri);
                    PhotoActivity.this.sendBroadcast(intent);//这个广播的目的就是更新图库,发了这个广播进入相册就可以找到你保存的图片了!,记得要传你更新的file哦
                }


                else ToastUtil.Show_SHORT(PhotoActivity.this, "图片保存失败");

                //对bitmap进行垃圾回收
                loadedImage.recycle();
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view) {

            }
        });


ImgUtil.saveFile() :

public static File saveFile(Bitmap bm, String fileName, String path)  {

		File myCaptureFile = null;
		try {
			String subForder = path;
			File foder = new File(subForder);
			if (!foder.exists()) {
				foder.mkdirs();
			}
			 myCaptureFile = new File(subForder, fileName);
			if (!myCaptureFile.exists()) {
				myCaptureFile.createNewFile();
			}
			BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
			bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
			bos.flush();
			bos.close();
		}catch (IOException e){

			e.printStackTrace();
		}

		return myCaptureFile;

	}






你可能感兴趣的:(imageLoader保存图片到本地相册)