我想浏览 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();
} }
}
}
}
最佳答案
试试这个:
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);
关于android - 从 SD 卡浏览和打开 pdf 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14019026/