浏览并打开sdcard的pdf文件?

时间:2012-12-24 08:35:51

标签: android pdf

我想浏览sdcard获取pdf文件,然后想在pdfviewer中打开它。无论我尝试打开SD卡,但不在pdfviewer中打开它。

我该怎么做?

public void onClick(View v) {

    Intent intent = new Intent();
    intent.setType("application/pdf");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Pdf"), REQUEST_PICK_FILE );

}
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent result) {
         if (resultCode == RESULT_OK) {
                switch (requestCode) {
                case REQUEST_PICK_FILE:
                {
                    Uri data = result.getData();

                    if(data.getLastPathSegment().endsWith("pdf")){
                        startActivity(new Intent(this, PDFReaderAct.class)); 
                    } else {
                        Toast.makeText(this, "Invalid file type", Toast.LENGTH_SHORT).show();   
                    }  }             
                }
            }
    }

4 个答案:

答案 0 :(得分:1)

试试这个:

File targetFile = new File(Environment.getExternalStorageDirectory(), "/Android/data/...../yourpdf.pdf");
Uri targetUri = Uri.fromFile(targetFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(targetUri, "application/pdf");
this.startActivity(intent);

答案 1 :(得分:0)

试试这段代码:

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

答案 2 :(得分:0)

试试这段代码。

Intent intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
intent.putExtra("CONTENT_TYPE", "*/*");
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(intent,1);

答案 3 :(得分:0)

这对我有用。如果您需要,可以取消注释以下几行。

 public void browsePdf(View v) {

    Intent intent = new Intent();
        intent.setType("application/pdf");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select PDF"), 1);

   }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent result) {
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case 1:
            {
                Uri data = result.getData();

             //   if(data.getLastPathSegment().endsWith("pdf")){
                    Intent intent_pdf=new Intent(this, PdfViewer.class);
                    intent_pdf.setData(data);
                    startActivity(intent_pdf);

              //  } else {
                //    Toast.makeText(this, "Invalid file type", Toast.LENGTH_SHORT).show();
              //  }
              }
        }
    }
}