首先我想说我已经尝试了几种解决方案、方法、建议并引用了此处的几个链接来打开 PDF。你可以说我慢,但我至少试过了。我想在我的 Android 应用程序中打开 PDF。我在 Nexus 10 平板电脑上。我无法使用 WebView 。我想通过我的一个 fragment 中的 OnClickListener 打开此 pdf。我认为我最大的问题是我不确定将我的 PDF 保存在哪里。我试过 res 和 assets 文件夹。许多示例使用/sdcard/- 只是将它保存在我的设备上吗?如果是这样,在哪里/如何获得路径?我在平板电脑上的 adobe reader 中保存了一个 .pdf 文件,我可以访问该路径吗?我正在使用 API min 16 目标 API 19。
我试过很多变体
public void onClick(View v)
{
switch(v.getId())
{
case R.id.bizbro3:
File pdfFile = new File( // I don't know what to put here / where to save pdf. Have tried /sdcard/ , getresrouces, absolutepath, ect.);
if(pdfFile.exists())
{
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(v.getContext(), "Something went wrong. Returning to the Main Menu",
Toast.LENGTH_LONG).show();
fragment = new FragmentThree();
fragment.setArguments(args);
FragmentManager frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, fragment)
.commit();
}
}
}
最佳答案
首先在manifest中声明权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
然后试试这个
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/yourfolder";
File file = new File(path,"file.pdf");
关于android - 在 Android App 中打开 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24538576/