在Android应用程序中阅读pdf

时间:2013-02-12 07:27:47

标签: java android

我正在制作一个应用程序,需要在Android应用程序中打开pdf而不使用任何第三方应用程序。我也不想在webview中查看我的pdf。为此我使用PdfViewer.jar并制作如下代码

java:

    public class MainActivity extends ListActivity{

        public class First extends ListActivity {

            String[] pdflist;
            File[] imagelist;
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                //setContentView(R.layout.main);

                File images = Environment.getExternalStorageDirectory();
                imagelist = images.listFiles(new FilenameFilter() {
                    public boolean accept(File dir, String name) {
                        return ((name.endsWith(".pdf")));
                    }
                });
                pdflist = new String[imagelist.length];
                for (int i = 0; i < imagelist.length; i++) {
                    pdflist[i] = imagelist[i].getName();
                }
                this.setListAdapter(new ArrayAdapter<String>(this,
                        android.R.layout.simple_list_item_1, pdflist));
            }

            protected void onListItemClick(ListView l, View v, int position, long id) {
                super.onListItemClick(l, v, position, id);
                String path = imagelist[(int) id].getAbsolutePath();
                openPdfIntent(path);
            }

            private void openPdfIntent(String path) {
                try {
                    final Intent intent = new Intent(First.this, Second.class);
                    intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
                    startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }}

Second.java



public class Second extends PdfViewerActivity 
{

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
}

public int getPreviousPageImageResource() {
    return R.drawable.left_arrow;
}

public int getNextPageImageResource() {
    return R.drawable.right_arrow;
}

public int getZoomInImageResource() {
    return R.drawable.zoom_in;
}

public int getZoomOutImageResource() {
    return R.drawable.zoom_out;
}

public int getPdfPasswordLayoutResource() {
    return R.layout.pdf_file_password;
}

public int getPdfPageNumberResource() {
    return R.layout.dialog_pagenumber;
}

public int getPdfPasswordEditField() {
    return R.id.etPassword;
}

public int getPdfPasswordOkButton() {
    return R.id.btOK;
}

public int getPdfPasswordExitButton() {
    return R.id.btExit;
}

public int getPdfPageNumberEditField() {
    return R.id.pagenum_edit;
}
}

我已经在android menifest中添加了我的second.java。但我的logcat显示以下错误.M没有收到错误

logcat:

02-12 09:23:50.408: E/ActivityThread(713): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40d8b368 that was originally bound here
02-12 09:23:50.408: E/ActivityThread(713):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
02-12 09:23:50.408: E/ActivityThread(713):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
02-12 09:23:50.408: E/ActivityThread(713):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
02-12 09:23:50.408: E/ActivityThread(713):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
02-12 09:23:50.408: E/ActivityThread(713):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
02-12 09:23:50.408: E/ActivityThread(713):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
02-12 09:23:50.408: E/ActivityThread(713):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
02-12 09:23:50.408: E/ActivityThread(713):  at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
02-12 09:23:50.408: E/ActivityThread(713):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
02-12 09:23:50.408: E/ActivityThread(713):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
02-12 09:23:50.408: E/ActivityThread(713):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
02-12 09:23:50.408: E/ActivityThread(713):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-12 09:23:50.408: E/ActivityThread(713):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-12 09:23:50.408: E/ActivityThread(713):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-12 09:23:50.408: E/ActivityThread(713):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-12 09:23:50.408: E/ActivityThread(713):  at java.lang.Thread.run(Thread.java:856)
02-12 09:23:50.455: E/StrictMode(713): null

任何肝脏?

2 个答案:

答案 0 :(得分:0)

在处理pdf逻辑的应用程序中使用适当的库。在您自己的应用程序中使用库时,不会启动其他应用程序来查看/修改pdf。

查看https://stackoverflow.com/questions/4665957/pdf-parsing-library-for-android以获取可用pdf库的摘要。

答案 1 :(得分:-2)

Use following code. But PDF viewer application must present on device.

 File pdfFile = new File(Environment.getExternalStorageDirectory()+ "/video/"+filePath); 
            if(pdfFile.exists()) 
            {
                Uri path = Uri.fromFile(pdfFile); 
                Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
                pdfIntent.setDataAndType(path, "application/pdf");
                pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                try
                {
                    startActivity(pdfIntent);
                }
                catch(ActivityNotFoundException e)
                {
                    Toast.makeText(this, "No Application available to view pdf", Toast.LENGTH_LONG).show(); 
                }