Viewpager Webview内存问题

Kir*_*ran 9 memory android webview

我正在使用一个viewpager来加载大约50个webviews ...所有的webview都加载到assests中,每个weview都有一个HTML页面,每个页面可以访问大约70个图像...当我轻扫时,我的应用程序在大约30页后崩溃,可能是webviews的cos仍然保持对assests文件夹中图像的引用...有没有办法释放Viewpager在那个特定时间没有使用的webview?

awesomePager.setAdapter(new AwesomePagerAdapter(this, webviewdata));
Run Code Online (Sandbox Code Playgroud)

细节:

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
Run Code Online (Sandbox Code Playgroud)

在viewpager上动态加载webview

logcat的: 在此输入图像描述

Vip*_*hah 1

尝试缩小位图。大多数时候位图是我们遇到内存问题的主要原因。另请了解如何回收位图。以下片段将对您有所帮助。

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 );
        }
Run Code Online (Sandbox Code Playgroud)

还要确保您确实重写了以下方法。

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

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

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();            
}       
Run Code Online (Sandbox Code Playgroud)