【问题标题】:How save image in Android Universal Image Loader?如何在 Android 通用图像加载器中保存图像?
【发布时间】:2014-08-18 16:18:30
【问题描述】:

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 中没有任何内容。有什么想法吗?

【问题讨论】:

  • new File("myFolder") 这不是您的应用可以访问的位置。你可能想使用context.getCacheDir()
  • 先使用context.getCacheDir(),然后再改成其他东西,但没有任何改变

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


【解决方案1】:

你必须把图片保存在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);
}

希望对你有帮助..

【讨论】:

  • 谢谢拉杰什。但我有一个大问题,惰性列表将图像保存在其默认文件夹中,而通用图像加载器是惰性列表的一个分支。它没有任何功能或其他东西可以自动执行此操作吗?
  • 如果我只需要在 PageAdapter 中保存当前的 VISIBLE 图像怎么办?
  • 嗨@Rajesh Mikkilineni 我关注了这个,但无法打开保存的图像。你知道是什么原因造成的吗?谢谢!
  • 请检查您保存的文件类型
【解决方案2】:

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

这是代码

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

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

【讨论】:

  • 更改此代码后,您是否可以使用 UIL 将图像保存到手机?
  • 太好了。我在 ImageLoaderConfiguration 中添加了这段代码,但没有保存它。能否请您简要介绍一下步骤?您是否进行了其他更改?
猜你喜欢
  • 1970-01-01
  • 2014-10-14
  • 2017-04-07
  • 2014-01-14
  • 2014-05-08
  • 1970-01-01
  • 2016-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多