Viewpager web视图内存问题视图、内存、问题、Viewpager

2023-09-04 04:00:10 作者:風逝去的泪痕

我使用的是viewpager装载约5​​0 webviews ......所有的webviews被加载到租入资产价值,每个weview都有一个访问大约70图像中的每个......至于我刷卡,我的应用程序崩溃后的HTML页大约30页,可能是webviews的COS仍然保持其参考的资产产生文件夹中的图片...有什么办法来释放该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

而动态加载的WebView上viewpager

while dynamically loading webview on viewpager

logcat的:

Logcat:

推荐答案

尝试缩放时间位图的下降bitmap.Most是我们得到的内存问题的一个主要原因。 还可以了解如何回收利用的位图。 下面的代码片段会帮助你。

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