如何在Android通用图像加载器中保存图像



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")))添加到config但是在myFolder里什么都没有。有什么想法吗?

必须将图片保存到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/ImageSDCard,使用
 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);
}

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

这里是代码

   DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .defaultDisplayImageOptions(defaultOptions)
                .build();

最新更新