【问题标题】:Android Universal Image Loader out of memory errorAndroid Universal Image Loader 内存不足错误
【发布时间】:2013-10-30 21:29:50
【问题描述】:

我正在使用通用图像加载器在网格视图中加载图像并加载图像预览,内存不足错误问题不断发生。我尝试了很多方法来更改配置、显示选项。但没有用。请给出正确的解决方法。

这是我使用通用图像加载器库显示图像的代码,

ImageLoader imageLoader = ImageLoader.getInstance();
        ImageLoaderConfiguration imageLoaderconfig = new ImageLoaderConfiguration.Builder(
                ImagePreview.this).threadPoolSize(1)
                .writeDebugLogs().memoryCache(new WeakMemoryCache())
                .build();
        imageLoader.init(imageLoaderconfig);
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showStubImage(R.drawable.empty_icon)
                .bitmapConfig(Config.RGB_565)
                .showImageForEmptyUri(R.drawable.image_for_empty_url)

                .imageScaleType(ImageScaleType.IN_SAMPLE_INT).build();

        imageLoader.displayImage("file://"
                + CommonVariables.preview_image_path, image_preview,
                options, new ImageLoadingListener() {

                    @Override
                    public void onLoadingStarted(String imageUri, View view) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onLoadingFailed(String imageUri, View view,
                            FailReason failReason) {
                        // TODO Auto-generated method stub
                        findViewById(R.id.pb_pd).setVisibility(
                                View.INVISIBLE); // progress bar
                        Toast.makeText(getApplicationContext(),
                                "Oops ! Please try again later",
                                Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onLoadingComplete(String imageUri,
                            View view, Bitmap loadedImage) {
                        // TODO Auto-generated method stub
                        Animation anim = AnimationUtils.loadAnimation(
                                ImagePreview.this, R.anim.fade_in);
                        findViewById(R.id.pb_pd).setVisibility(
                                View.INVISIBLE); // progress bar
                        image_preview.setAnimation(anim);
                        anim.start();
                    }

                    @Override
                    public void onLoadingCancelled(String imageUri,
                            View view) {
                        // TODO Auto-generated method stub
                        findViewById(R.id.pd).setVisibility(View.INVISIBLE); // progress bar
                    }
                });

例外:(原木猫)

10-22 12:30:00.414: E/AndroidRuntime(6908): FATAL EXCEPTION: main
10-22 12:30:00.414: E/AndroidRuntime(6908): java.lang.OutOfMemoryError
10-22 12:30:00.414: E/AndroidRuntime(6908):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
10-22 12:30:00.414: E/AndroidRuntime(6908):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:546)
10-22 12:30:00.414: E/AndroidRuntime(6908):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:320)
10-22 12:30:00.414: E/AndroidRuntime(6908):     at com.doodleblue.dateitpro.Home.decodeSampledBitmapFromUri(Home.java:669)
10-22 12:30:00.414: E/AndroidRuntime(6908):     at com.doodleblue.dateitpro.Home.addDateTime(Home.java:413)
10-22 12:30:00.414: E/AndroidRuntime(6908):     at com.doodleblue.dateitpro.Home.callmethods(Home.java:356)
10-22 12:30:00.414: E/AndroidRuntime(6908):     at com.doodleblue.dateitpro.Home.onCreate(Home.java:236)
10-22 12:30:00.414: E/AndroidRuntime(6908):     at android.app.Activity.performCreate(Activity.java:5020)
10-22 12:30:00.414: E/AndroidRuntime(6908):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)

【问题讨论】:

  • 你在哪里显示图像?
  • 你的代码和错误在哪里?
  • @GrIsHu 我的代码贴出来了..

标签: android out-of-memory universal-image-loader


【解决方案1】:

尝试创建图像的缩略图。这对我有用

final int THUMBSIZE = 64;
Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), 
                    THUMBSIZE, THUMBSIZE);

【讨论】:

    【解决方案2】:

    当图像加载到虚拟内存较少时会发生此错误。 这可以通过创建位图缩放图像来解决。 将所有可绘制对象放在一个数组中,然后像这样创建它的重新缩放位图。

    Bitmap bit = BitmapFactory.decodeResource(getApplicationContext()
                .getResources(), drawable);
        if (bit != null) {
            Bitmap resizedBitmap = Bitmap.createScaledBitmap(bit, width,
                    height, true);
            imageView.setBitmap(resizedBitmap);
            }
    

    重新缩放的位图不会占用更多的虚拟内存并且可以轻松加载图像

    BitmapDrawable bd=(BitmapDrawable) this.getResources().getDrawable(R.drawable.icon);
    int height=bd.getBitmap().getHeight();
    int width=bd.getBitmap().getWidth();
    

    【讨论】:

    • 是的,我这里用的是这种方式,怎样才能得到图片的准确宽度和高度呢?
    • 检查编辑的答案。这是获取宽度和高度的方法。
    • 只取宽高为150dp并以网格视图显示。这一定会对你有所帮助。
    猜你喜欢
    • 2013-03-15
    • 2013-06-17
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多