从内部存储的下载文件夹中打开下载的文件 (pdf)
Open a Downloaded file (pdf) from the Downloads folder in the INTERNAL STORAGE
我已经使用下载管理器下载了一个文件 (133465.pdf),现在它存储在手机 phone(内部存储)的下载文件夹中。
我应该如何尝试从“下载”文件夹中检索下载的 pdf?
我正在使用以下代码尝试从下载文件夹中检索 pdf,但我在 Toast 上收到错误,显示 "Cannot display PDF (133465.pdf cannot be opened)"。
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);
我不知道我是否使用了正确的文件位置来访问文件。
如有任何帮助或建议,我们将不胜感激。
如果您为 Lollopop 及以下公司工作:您不必在 运行 时间请求用户许可。清单权限即可。
如果您为 Marshmellow 及更高版本工作:您必须在 运行 时向用户询问 permission,并根据用户的输出采取行动。
记住:您还必须在清单上授予权限。
要在用户下载文件夹中下载 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);
要从用户设备的下载文件夹中查看下载的 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) {
}
注意:在下载查看 Marshmellow 和 UP 的 PDF 文件之前,请确保您获得了授予的权限。
我已经使用下载管理器下载了一个文件 (133465.pdf),现在它存储在手机 phone(内部存储)的下载文件夹中。
我应该如何尝试从“下载”文件夹中检索下载的 pdf?
我正在使用以下代码尝试从下载文件夹中检索 pdf,但我在 Toast 上收到错误,显示 "Cannot display PDF (133465.pdf cannot be opened)"。
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);
我不知道我是否使用了正确的文件位置来访问文件。
如有任何帮助或建议,我们将不胜感激。
如果您为 Lollopop 及以下公司工作:您不必在 运行 时间请求用户许可。清单权限即可。
如果您为 Marshmellow 及更高版本工作:您必须在 运行 时向用户询问 permission,并根据用户的输出采取行动。
记住:您还必须在清单上授予权限。
要在用户下载文件夹中下载 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);
要从用户设备的下载文件夹中查看下载的 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) {
}
注意:在下载查看 Marshmellow 和 UP 的 PDF 文件之前,请确保您获得了授予的权限。