【发布时间】:2018-11-11 08:17:13
【问题描述】:
我已使用此代码在我的应用程序中下载 pdf 文件,它适用于所有手机,但我编写的用于打开下载文件的代码适用于小于 21 的 APK,但不适用于更好的手机,例如三星S7等
我下载pdf文件的代码:
Boolean memoerAvailable = (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED));
if (memoerAvailable) {
final DownloadManager.Request request = new DownloadManager.Request(Uri.parse(link));
request.setTitle("Click to download the book");
request.setDescription("File is being downloaded...");
//use download manager to download the clicked book
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, ShowbooksActivity.myBooksList.get(ShowbooksActivity.pos).getMpdfName());
request.setDestinationInExternalFilesDir(bookWithAdapterActivity.this, Environment.DIRECTORY_DOWNLOADS, "myfile.jpg");
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long queueid = manager.enqueue(request);
我打开下载文件的代码:
filetoopen = ls1.get(position);
File file = new File(Environment.getExternalStorageDirectory() +"/Download/" + filetoopen);
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getContext(), "File not found", Toast.LENGTH_SHORT).show();
}
【问题讨论】:
-
您遇到什么错误?有错误信息吗?