0

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

爪哇:

    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 清单中添加了我的 second.java。但我的 logcat 显示以下错误。M 没有收到错误

日志猫:

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

有帮助吗??

4

2 回答 2

0

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

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

于 2013-02-12T07:48:57.233 回答
-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(); 
                }
于 2013-02-12T09:56:26.647 回答