android - 使用 Raw 文件夹中存在的 adobe reader 打开 pdf
问题描述
我试图显示存在于 android studio 的 raw 文件夹中的 pdf 文件。我按照下面的代码。但这显示错误 android.content.ActivityNotFoundException: No Activity found to handle Intent 如何解决这个问题?
String uri = "raw://" + R.raw.science;
Uri path = Uri.fromFile(new File(String.valueOf(uri)));
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(pdfIntent);
解决方案
首先:将您的 pdf 从 raw 复制到 sd 卡。
确保在 sd 卡中复制 pdf 的权限:
WRITE_EXTERNAL_STORAGE和
READ_EXTERNAL_STORAGE
示例代码:
public static String dirPath = Environment.getExternalStorageDirectory() + "/ExampleDir/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CopyPDFFromAssets();
}
做一个方法:CopyPDFFromAssets()
private void CopyPDFFromAssets() {
copyFile(getResources().openRawResource(R.raw.science),
new FileOutputStream(new File(dirPath, "my_pdf.pdf")));
File pdfFile = new File(dirPath, "my_pdf.pdf");
Uri path = Uri.fromFile(pdfFile);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, "application/pdf");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
} catch (Exception ex) {
ex.toString();
}
}
然后创建方法copyFile()
::
private void copyFile(InputStream in , OutputStream out) throws IOException {
try {
byte[] buffer = new byte[1024];
int read;
while ((read = in .read(buffer)) != -1) {
out.write(buffer, 0, read);
}
} catch (Exception exp) {
}
}
推荐阅读
- javascript - 两个循环之间的区别
- keras - Colab Kerastuner INFO:tensorflow:Reloading Oracle from existing project ./untitled_project/oracle.json
- html - 如何使用 bootstrap-3 移动卡片中的项目
- html - 标题不会与视频背景一起显示:
- assembly - 是否可以使用内部宏使用的外部宏参数?
- reactjs - React - 通过单击按钮获取输入的值
- apache-kafka - zookeeper 是否仅用于 kafka,如果其他人可以给出实时示例?
- reactjs - 输入不使用钩子重新渲染 onChange
- wpf - Material Design (WPF):禁用复选框动画
- android - 如何在android 10中验证wifi密码并连接/断开特定wifi网络