Viewpager Webview 内存问题 [英] Viewpager Webview memory issue

查看:14
本文介绍了Viewpager Webview 内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 viewpager 加载大约 50 个 webviews...所有 webviews 都加载到资产中,每个 weview 都有一个 HTML 页面,每个页面访问大约 70 个图像...当我刷卡时,我的应用程序崩溃了大约 30 个页面,可能是因为 webviews 仍然保留对assests 文件夹中图像的引用......有没有办法释放 Viewpager 在那个特定时间没有使用的 webviews?

I'm using a viewpager to load around 50 webviews... All the webviews are loaded into assests and each weview has an HTML page that is accessing around 70 images each... As i swipe, my app is crashing after around 30 pages, might be cos of the webviews still keep their reference to the images in the assests folder... Is there any way to release the webviews that the Viewpager is not using at that particular time?

awesomePager.setAdapter(new AwesomePagerAdapter(this, webviewdata));

详情:

Android WebView Memory Leak when loading html file from Assets  
Failed adding to JNI local ref table (has 512 entries)
"Thread-375" prio=5 tid=15 RUNNABLE

同时在 viewpager 上动态加载 webview

while dynamically loading webview on viewpager

日志猫:

推荐答案

尝试缩小位图.大多数时候位图是我们出现内存问题的主要原因.还可以了解如何回收位图.以下代码段将对您有所帮助.

Try to scale down bitmap.Most of the time Bitmap is a main reason we get Memory issue. Also learn about how to recycle the bitmaps. Following snippet will help you.

BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile( filename, options );
        options.inJustDecodeBounds = false;
        options.inSampleSize = 2; 

        bitmap = BitmapFactory.decodeFile( filename, options );
        if ( bitmap != null && exact ) {
            bitmap = Bitmap.createScaledBitmap( bitmap, width, height, false );
        }

还要确保您确实覆盖了以下方法.

Also make sure you did override following method.

@Override
public void destroyItem(View collection, int position, Object view) {
    ((ViewPager) collection).removeView((TextView) view);
}

或者你可以创建一个函数来缩小位图

Or you can create a function to Scale Down Bitmap

private byte[] resizeImage( byte[] input ) {

    if ( input == null ) {
        return null;
    }

    Bitmap bitmapOrg = BitmapFactory.decodeByteArray(input, 0, input.length);

    if ( bitmapOrg == null ) {
        return null;
    }

    int height = bitmapOrg.getHeight();
    int width = bitmapOrg.getWidth();
    int newHeight = 250;

    float scaleHeight = ((float) newHeight) / height;

    // creates matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleHeight, scaleHeight);

    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
            width, height, matrix, true);

    bitmapOrg.recycle();

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    resizedBitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);            

    resizedBitmap.recycle();

    return bos.toByteArray();            
}       

这篇关于Viewpager Webview 内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆