Suh*_*vez 4 pdf android file android-intent android-download-manager
我已经使用下载管理器下载了一个文件(133465.pdf),现在它存储在手机的下载文件夹中(内部存储)。
我应该如何尝试从下载文件夹中检索下载的 pdf?
我正在使用以下代码尝试从下载文件夹中检索 pdf,但在 Toast 上出现错误,提示“无法显示 PDF(133465.pdf 无法打开)”。
String file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + File.separator + "133465.pdf";
Log.i("Fragmentadapter1", file);
File videoFile2Play = new File(file);
Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(videoFile2Play), "application/pdf");
imageContext.startActivity(i);
Run Code Online (Sandbox Code Playgroud)
我不知道我是否使用正确的文件位置来访问文件。
任何帮助或建议将不胜感激。
如果您正在为 Lollopop 及以下公司工作:您不必向用户询问运行时的许可。清单权限会做。
如果您为 Marshmellow 及更高版本工作:您必须询问用户对运行时的许可,并根据用户的输出采取行动。
请记住:您仍然必须在清单上授予权限。
要在用户下载文件夹中下载 PDF:
DownloadManager downloadmanager;
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
.mkdirs();
downloadmanager = (DownloadManager) getApplication().getSystemService(Context.DOWNLOAD_SERVICE);
String url = hardcode + bb ;
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri)
.setTitle(bb )
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
bb)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Log.i("Download1", String.valueOf(request));
downloadmanager.enqueue(request);
Run Code Online (Sandbox Code Playgroud)
从用户设备的下载文件夹查看下载的 PDF:
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + File.separator +
"YOUR FILE NAME");
Uri path = Uri.fromFile(file);
Log.i("Fragment2", String.valueOf(path));
Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pdfOpenintent.setDataAndType(path, "application/pdf");
try {
this.startActivity(pdfOpenintent);
} catch (ActivityNotFoundException e) {
}
Run Code Online (Sandbox Code Playgroud)
注意:在下载 Marshmellow 和 UP 的查看 PDF 文件之前,请确保您获得了授予的权限。
| 归档时间: |
|
| 查看次数: |
8628 次 |
| 最近记录: |