0

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.任何想法?

4

2 回答 2

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);
}

希望对你有帮助。。

于 2014-08-18T17:56:40.597 回答
4

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

这是代码

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

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .defaultDisplayImageOptions(defaultOptions)
                .build();
于 2014-08-19T05:01:59.833 回答