如何在Android Universal Image Loader中保存图像?

时间:2014-08-18 16:18:31

标签: android android-imageview android-image universal-image-loader android-lazyadapter

Lazy-List我使用此代码和我的图片存储在名为LazyList

的文件夹中

ImageLoader imageLoader=new ImageLoader(context); imageLoader.DisplayImage(url, imageView);

但在Universal-Image-Loader我使用此

 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())    
            .build();
    ImageLoader.getInstance().init(config);
    img = (ImageView) this.findViewById(R.id.test_id);
    ImageLoader imageLoader = ImageLoader.getInstance();
    imageLoader.displayImage(URL.RECIPE_IMG_URL,img);

但每次使用互联网获取图片并且没有将我的图片存储在任何文件夹中我都会.diskCache(new UnlimitedDiscCache(new File("myFolder")))添加到configmyFolder中没有任何内容。任何想法?

2 个答案:

答案 0 :(得分:5)

您必须在onLoadingComplete

上保存图片

尝试此操作,在onLoadingComplete上,您必须将bitmap保存到变量bitmap

imageLoader.getInstance().displayImage(imagePath, imageView, options, new SimpleImageLoadingListener() {
        @Override
        public void onLoadingStarted(String imageUri, View view) {
            spinner.setVisibility(View.VISIBLE);
        }

        @Override
        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
            String message = null;
            switch (failReason.getType()) {
            case IO_ERROR:
                message = "Input/Output error";
                break;
            case DECODING_ERROR:
                message = "Image can't be decoded";
                break;
            case NETWORK_DENIED:
                message = "Downloads are denied";
                break;
            case OUT_OF_MEMORY:
                message = "Out Of Memory error";
                break;
            case UNKNOWN:
                message = "Unknown error";
                break;
            }


        }

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

            showedImgae = loadedImage;

        }
    });

现在点击保存按钮/如果要将Bitmap / Image保存到SDCard使用此

 public void downloadImage(){

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/DCIM/youfoldername");    
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "imagename-"+ n +".jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
        FileOutputStream out = new FileOutputStream(file);
        showedImgae.compress(Bitmap.CompressFormat.JPEG, 100, out);
        Toast.makeText(MyActivity.this, "Image Saved", Toast.LENGTH_SHORT).show();
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri contentUri = Uri.fromFile(file);
    mediaScanIntent.setData(contentUri);
    getApplicationContext().sendBroadcast(mediaScanIntent);
}

希望它可以帮助你..

答案 1 :(得分:4)

我找到了答案。默认情况下,缓存是禁用的,我应该将DisplayImageOptions添加到ImageLoaderConfiguration

这是代码

   DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .defaultDisplayImageOptions(defaultOptions)
                .build();