单击列表视图项使用第三方打开Pdf文件

时间:2014-04-09 11:38:59

标签: android listview pdf assets

我的资源文件夹中有PDF文件,现在我可以在列表视图中查看PDF文件。但现在问题是点击任何我希望在我的pdf查看器中打开pdf的pdf。 这是我的代码

    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AssetManager asset = getAssets();
        try {
            final String[] arrdata = asset.list("PDFfolder");
            List<String> pdflist = new ArrayList<String>();
            int size = arrdata.length;
            for(int i = 0;i<size;i++)
            {
              if(arrdata[i].contains(".pdf"))

              {
                pdflist.add(arrdata[i]); 
               }
            }
            ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,pdflist);
            ListView listView = (ListView) findViewById(R.id.listView1);
            listView.setAdapter(adapter);
                    listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
             public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                if(position == 0 ) {
                    File pdffile = new File("file:///android_assets/AAI.pdf");
                    //File ff = new File(getAssets().open("AAI.pdf"));
                     Uri path = Uri.fromFile(pdffile);
                     Intent intent = new Intent(Intent.ACTION_VIEW);
                     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                     intent.setDataAndType(path, "application/pdf");
                     startActivity(intent);    
                     }

                }

        });
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

     }
}

现在请帮我解决如何使用我的资源文件夹中的意图打开它。我发现没有找到处理意图的活动的错误,因为我手机里已经有pdfviewer了。

2 个答案:

答案 0 :(得分:0)

您需要安装一个应用来处理打开PDF。您应该使用Intent Chooser,如下所示:

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Intent intent = Intent.createChooser(target, "Open File");
try {
    startActivity(intent);
} catch (ActivityNotFoundException e) {
    // Instruct the user to install a PDF reader here, or something
}   

资产目录中的文件未解包。相反,它们直接从APK ZIP文件中读取。

所以,你真的不能制作需要文件接受资产'文件'的东西。

相反,您必须提取资产并将其写入单独的文件,如下所示:

File f = new File(getCacheDir()+"/fileName.pdf");
  if (!f.exists()) try {

    InputStream is = getAssets().open("fileName.pdf");
    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);
    is.close();


    FileOutputStream fos = new FileOutputStream(f);
    fos.write(buffer);
    fos.close();
  } catch (Exception e) { throw new RuntimeException(e); }

您还可以使用AssetManager来管理资源文件夹中的文件。在活动中,请调用getAssets ()方法获取AssetManager实例。

希望它对你有所帮助。

答案 1 :(得分:0)

在我的项目中完美地运行尝试此代码

/**
         * get on item click listener
         */
        userList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                                    final int position, long id) {

                TextView tv_paper_name = (TextView) v.findViewById(R.id.tv_paper_name);
                String PaperName = tv_paper_name.getText().toString();
                File extStore = Environment.getExternalStorageDirectory();
                File myFile = new File(extStore.getAbsolutePath() + "/Exam Papers/"+PaperName+".pdf");

                if (myFile.exists()) {

                File pdfFile = new File(Environment.getExternalStorageDirectory() + "/Exam Papers/" +PaperName+".pdf");  // -> filename
                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(MainActivity.this, "No Application available to view PDF", Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(MainActivity.this, "Download file first", Toast.LENGTH_SHORT).show();
                }


            }
        });