我用它来下载文件:
String url = "https://File To Download";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("My File To Download");
request.setTitle("Downloading");
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, context.getString(R.string.dloded_latest_numbers));
// get download service and enqueue file
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
哪一切都很棒。将文件放在我确认的标准下载目录中。
然后我尝试将文件读入BufferedReader,如下所示:
File latestNumbersFile;
latestNumbersFile = new File(Environment.DIRECTORY_DOWNLOADS, getString(R.string.dloded_latest_numbers));
TextView displayArray = (TextView) findViewById(R.id.currentNumbers);
String tmp = latestNumbersFile.toString(); // Converts type (File) to string type
displayArray.setText(tmp);
BufferedReader buffedReader = new BufferedReader(new FileReader(latestNumbersFile));
然后在TextView中显示文件位置,它可以工作并提供目录/下载/文件下载 注意领先的“/”不确定这是否重要......
我的问题是: 使用File(Environment.DIRECTORY_DOWNLOADS,getString(R.string.dloded_latest_numbers)允许我下载到目录,我希望文件在,但是使用完全相同的引用来读取文件,我得java.io.FileNotFoundException打开失败:ENOENT(没有这样的文件或目录)
任何人都可以帮忙吗?
答案 0 :(得分:1)
好的,你只要求android.permission.WRITE_EXTERNAL_STORAGE
许可,但你没有权限从SD卡读取。你还需要添加
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />