简体   繁体   English

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

[英]How save image in Android Universal Image Loader?

In Lazy-List i use this code and my image store in a folder named LazyList Lazy-List我使用此代码并将图像存储在名为LazyList的文件夹中

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

But in Universal-Image-Loader i use this 但是在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);

but each time it use internet to get image and didn't store my image in any folder i aslo add .diskCache(new UnlimitedDiscCache(new File("myFolder"))) to config but nothing store in myFolder .Any ideas? 但是每次它使用互联网获取图像并且没有将我的图像存储在任何文件夹中时,我也添加.diskCache(new UnlimitedDiscCache(new File("myFolder")))进行config但是myFolder没有存储任何内容。

You have to save the Image on onLoadingComplete 您必须将图像保存在onLoadingComplete

try this , on onLoadingComplete you have to save the bitmap to a variable bitmap 试试看,在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;

        }
    });

now onclick on save Button / if you want to save that Bitmap / Image to SDCard use this 现在on点击保存按钮/如果您想将该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);
}

Hope it helps you.. 希望对您有帮助。

I found my answer . 我找到了答案。 By default caching is disable i should add DisplayImageOptions to ImageLoaderConfiguration 默认情况下禁用缓存,我应该将DisplayImageOptions添加到ImageLoaderConfiguration

And here is code 这是代码

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM