【问题标题】:Open Pdf file using third party on click on list view item单击列表视图项使用第三方打开 Pdf 文件
【发布时间】:2014-04-09 11:38:59
【问题描述】:

我的资产文件夹中有 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,所以我没有发现任何活动来处理意图的错误。

【问题讨论】:

标签: android listview pdf assets


【解决方案1】:

您需要安装一个应用程序来处理打开 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 来管理资产文件夹中的文件。在activity中调用getAssets ()方法获取AssetManagerInstance。

希望对你有帮助。

【讨论】:

  • 如果不提取就不可能......因为我在这里读过stackoverflow.com/questions/13517412/…这里他们也没有给出解决方案你能看看我的代码并告诉我我会有什么吗做什么?任何帮助都将不胜感激
  • 谢谢@Shruti ...但是我在列表视图中有pdf名称我必须在点击不同名称时打开不同的pdf...
【解决方案2】:

在我的项目中完美运行试试这个代码

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


            }
        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    • 2020-11-13
    相关资源
    最近更新 更多