如何在viewpager中处理内存管理

时间:2014-10-16 14:57:13

标签: android pdf

我创建了一个使用view pager显示pdf文件的应用程序。每个pdf文件只包含一个页面。我尝试使用view pager加载20个pdf文件。但是当我浏览pdf文件时,它给出了outofmemory错误。

10-16 14:48:18.410: E/Inside onCreateView(12317): 1.pdf
10-16 14:48:24.030: E/dalvikvm-heap(12317): Out of memory on a 2961936-byte allocation.
10-16 14:48:24.040: E/AndroidRuntime(12317): FATAL EXCEPTION: main
10-16 14:48:24.040: E/AndroidRuntime(12317): java.lang.OutOfMemoryError
10-16 14:48:24.040: E/AndroidRuntime(12317):    at android.graphics.Bitmap.nativeCreate(Native Method)
10-16 14:48:24.040: E/AndroidRuntime(12317):    at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
10-16 14:48:24.040: E/AndroidRuntime(12317):    at android.graphics.Bitmap.createBitmap(Bitmap.java:585)
10-16 14:48:24.040: E/AndroidRuntime(12317):    at com.epapyrus.plugpdf.core.viewer.PageView.adjustPatch(PageView.java:352)
10-16 14:48:24.040: E/AndroidRuntime(12317):    at com.epapyrus.plugpdf.core.viewer.SinglePageDisplay.onUserInteractionComplete(SinglePageDisplay.java:107)
10-16 14:48:24.040: E/AndroidRuntime(12317):    at com.epapyrus.plugpdf.core.viewer.SinglePageDisplay$2.run(SinglePageDisplay.java:219)
10-16 14:48:24.040: E/AndroidRuntime(12317):    at android.os.Handler.handleCallback(Handler.java:605)
10-16 14:48:24.040: E/AndroidRuntime(12317):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-16 14:48:24.040: E/AndroidRuntime(12317):    at android.os.Looper.loop(Looper.java:137)
10-16 14:48:24.040: E/AndroidRuntime(12317):    at android.app.ActivityThread.main(ActivityThread.java:4507)
10-16 14:48:24.040: E/AndroidRuntime(12317):    at java.lang.reflect.Method.invokeNative(Native Method)
10-16 14:48:24.040: E/AndroidRuntime(12317):    at java.lang.reflect.Method.invoke(Method.java:511)
10-16 14:48:24.040: E/AndroidRuntime(12317):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
10-16 14:48:24.040: E/AndroidRuntime(12317):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
10-16 14:48:24.040: E/AndroidRuntime(12317):    at dalvik.system.NativeStart.main(Native Method)

以下是我的网页浏览器类

package com.android.pageviewers;

import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PageViewActivity extends FragmentActivity implements Communicator{
    MyPageAdapter pageAdapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page_view);

       // viewPdf();

        List<Fragment> fragments = getFragments();        
        pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);        
        ViewPager pager = (ViewPager)findViewById(R.id.pager);
       // ViewPager pager = new ViewPager(this);
      //  pager.setId( R.id.pager ); 
        pager.setAdapter(pageAdapter);

    }

    /*
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.activity_page_view, container, false);
        List<Fragment> fragments = getFragments();   
        pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);        
        ViewPager pager = (ViewPager)contentView.findViewById(R.id.pager);
        pager.setAdapter(pageAdapter);
        return contentView;
    }
    */

    private List<Fragment> getFragments(){

        List<Fragment> fList = new ArrayList<Fragment>();       
        fList.add(MyFragment2.newInstance("Fragment 1"));
        fList.add(MyFragment2.newInstance("Fragment 2"));
        fList.add(MyFragment2.newInstance("Fragment 3"));

        return fList;
    }



    private class MyPageAdapter extends FragmentPagerAdapter {
        private List<Fragment> fragments;

        public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }
        @Override
        public Fragment getItem(int position) {
            return this.fragments.get(position);
        }

        @Override
        public int getCount() {
            return this.fragments.size();
        }
    }

}
下面的

是fragment2类

package com.android.plugpdfsample;

import java.io.File;
import java.io.InputStream;

import com.epapyrus.plugpdf.SimpleDocumentReader;
import com.epapyrus.plugpdf.SimpleDocumentReaderListener;
import com.epapyrus.plugpdf.SimpleReaderFactory;
import com.epapyrus.plugpdf.core.viewer.DocumentState;
import com.epapyrus.plugpdf.core.viewer.ReaderView;

import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyFragment extends Fragment {
    public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
    // create a listener for receiving provide pdf loading results
                SimpleDocumentReaderListener m_listener = new SimpleDocumentReaderListener() {

                    @Override
                    public void onLoadFinish(DocumentState.OPEN state) {
                    }
                };


    public static final MyFragment newInstance(String message)
    {
        MyFragment f = new MyFragment();
        Bundle bdl = new Bundle(1);
        bdl.putString(EXTRA_MESSAGE, message);
        f.setArguments(bdl);
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        s");

        Log.i("onCreateView1", "startView v = inflater.inflate(R.layout.myfragment_layout, container, false);

        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
        File fl =new File(Environment.getExternalStorageDirectory(), "/1.pdf");
        ReaderView readerView = (ReaderView) v.findViewById(R.id.pdfview);
        readerView.openFile(fl.toString(),"");

        Log.i("onCreateView1", "ends");

        return v;
    }

}

下面是pdf文件的xml标签

 <com.android.ab.custom.PDFView 
            android:id="@+id/pdfview"
            android:background="#FF0000FF"
            android:padding="5dp"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

2 个答案:

答案 0 :(得分:1)

显然。随着更多的活动,碎片,图像,......你需要更多的记忆。而不是加载20 ViewPager尝试仅加载3并在ListView工作时回收它们。

我因为这个说了3:

ViewPager 1 -> The previous loaded PDF
ViewPager 2 -> Current and visible loaded PDF
ViewPager 3 -> The next loaded PDF

用户滚动后,您只需重新加载这三个ViewPager。

答案 1 :(得分:1)

只需在片段中的onDestroyView()方法中关闭打开的pdf文件。