gpt4 book ai didi

android - 从 SD 卡浏览和打开 pdf 文件?

转载 作者:行者123 更新时间:2023-11-29 01:58:09 26 4
gpt4 key购买 nike

我想浏览 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com