2

我想浏览 sdcard 中的 pdf 文件,然后想在 pdfviewer 中打开它。无论我尝试打开 sdcard 但没有在 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

4 回答 4

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);
于 2012-12-24T08:37:49.670 回答
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);
于 2012-12-24T08:37:22.637 回答
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();
              //  }
              }
        }
    }
}
于 2016-01-03T19:09:13.827 回答
0

试试这个代码。

Intent intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
intent.putExtra("CONTENT_TYPE", "*/*");
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(intent,1);
于 2015-08-06T13:50:58.943 回答