Java 在Android上显示PDF文件

Java 在Android上显示PDF文件,java,android,pdf,Java,Android,Pdf,我想加载和显示PDF文件。这些文件位于列表视图中,我希望显示选定的PDF文件。代码如下: public class MainActivity extends ActionBarActivity { final StringBuffer sb = new StringBuffer(); private ListView mainListView ; private ArrayAdapter<String> listAdapter ;

我想加载和显示PDF文件。这些文件位于列表视图中,我希望显示选定的PDF文件。代码如下:

 public class MainActivity extends ActionBarActivity {

        final StringBuffer sb = new StringBuffer();
    private ListView mainListView ;  
        private ArrayAdapter<String> listAdapter ;  
    String filepath;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_firstactivity);
            mainListView = (ListView) findViewById( R.id.mainListView );  
            final ArrayList<String> List = new ArrayList<String>();  
        final File storage = Environment.getExternalStorageDirectory();
            File file = new File(storage,"/Folder/");
        if (file.exists() && file.isDirectory()) {
                for (String s : file.list()) {
                    sb.append(s + " ");
        List.addAll( Arrays.asList(s) );
                }
            }

            listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, R.id.rowTextView,List); 
            mainListView.setAdapter( listAdapter );  
    mainListView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                    String filepath = new File("/Folder/"+ List.get(arg2)).getAbsolutePath();
                     File file = new File(filepath);
                    openPdfIntent(file);


                }


            });
        }
        private void openPdfIntent(File file) {
               Uri path = Uri.fromFile(file);

                            Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
                            pdfIntent.setDataAndType(path, "application/pdf");
                            pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            try {
                                startActivity(pdfIntent);
                            } catch (Exception e) {
                            e.printStackTrace();
                        }
     }
公共类MainActivity扩展了ActionBarActivity{
最终StringBuffer sb=新StringBuffer();
私有ListView主ListView;
专用阵列适配器列表适配器;
字符串文件路径;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u firstactivity);
mainListView=(ListView)findViewById(R.id.mainListView);
最终ArrayList=新ArrayList();
final File storage=Environment.getExternalStorageDirectory();
File File=新文件(存储“/Folder/”;
if(file.exists()&&file.isDirectory()){
对于(字符串s:file.list()){
某人附加(s+);
List.addAll(Arrays.asList);
}
}
listAdapter=new ArrayAdapter(this,R.layout.simplerow,R.id.rowTextView,List);
mainListView.setAdapter(listAdapter);
mainListView.setOnItemClickListener(新的OnItemClickListener(){
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
字符串filepath=newfile(“/Folder/”+List.get(arg2)).getAbsolutePath();
文件文件=新文件(文件路径);
openPdfIntent(文件);
}
});
}
私有void openPdfIntent(文件){
Uri路径=Uri.fromFile(文件);
Intent pdfIntent=新意图(Intent.ACTION\u视图);
setDataAndType(路径,“application/pdf”);
pdfIntent.setFlags(意图、标志、活动、清除、顶部);
试一试{
星触觉;
}捕获(例外e){
e、 printStackTrace();
}
}
此代码无效。logcat错误为:“java.lang.NullPointerException”。 如何解决此问题?

请尝试此代码:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("path-to-document"));
intent.setType("application/pdf");
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
if (activities.size() > 0) {
    startActivity(intent);
} else {
    // Do something else here. Maybe pop up a Dialog or Toast
}
Intent-Intent=newintent(Intent.ACTION_视图,Uri.parse(“文档路径”);
intent.setType(“application/pdf”);
PackageManager pm=getPackageManager();
列出活动=pm.querytentActivities(intent,0);
如果(activities.size()>0){
星触觉(意向);
}否则{
//在这里做点别的。也许弹出一个对话或祝酒词
}

据我所知,您得到的错误是由于文件路径不正确,仅指定“/Folder/”将返回空指针异常。 试试像这样的东西

       String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/FolderName/"+"File name in list"

试试这个例子,旧的类型,但代码是完美的

请发布整个堆栈跟踪,并指出该堆栈跟踪中的哪些行是上面显示的源代码中的行。否则,我们无法很好地知道生成
NullPointerException的行是哪一行。
String filepath=new File(“/Folder/”+List.get(arg2)).getAbsolutePath()
,此行不正确。我如何指示选定的文件?但是路径文件写得很好,因为我在另一个应用程序中使用了它,它可以工作。我如何从路径获取文件?PackageManager和List无法解析为typePackageManager是android类:ResolveInfo是android类,您只需要导入它们