简体   繁体   中英

reading pdf within the android app

I am making an application which require to open pdf within the android app without using any third party application.also i don't want to view my pdf in webview .For this i use PdfViewer.jar and made a code like below.

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

i have added my second.java in android menifest too.But my logcat is showing the following error.M not getting the error

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

Any hepl??

Use a proper library in your application handling the pdf logic. When using a library inside your own application no other application is started to view/modify the pdf.

Take a look at https://stackoverflow.com/questions/4665957/pdf-parsing-library-for-android for a summary of available pdf libraries.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM