我想从http网址下载文件,然后将其保存在sdCard或内部存储中(如果sdCard不存在),但是我对.getExternalStorage的输出以及如何访问特殊内部目录感到困惑。 我写了这段代码来检查sdCard的存在,然后检查下载文件的setDestination:
public void checkForDownload() {
Boolean isSdCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
Boolean isSdSupportedDevice = Environment.isExternalStorageRemovable();
if (isSdCard && isSdSupportedDevice) {
File sdDirectory = new File(Environment.getExternalStorageDirectory() + "pdfFiles", fileName);
if (!sdDirectory.exists()) {
downloadpdfBook(myActivity.this, "/pdfFiles", true);
} else
Toast.makeText(myActivity.this, "This file is downloaded", Toast.LENGTH_SHORT).show();
} else {
File directory = new File(myActivity.this.getFilesDir() + "pdfFiles", fileName);
if (!directory.exists()) {
downloadpdfBook(myActivity.this, "/pdfFiles", false);
} else
Toast.makeText(myActivity.this, "This file is downloaded", Toast.LENGTH_SHORT).show();
}
}
public void downloadpdfFile(Context context, String directory, Boolean isSd) {
Uri uri = Uri.parse(bookUrl);
downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
if (isSd) {
request.setDestinationInExternalPublicDir(Environment.getExternalStorageDirectory() + directory, fileName);
} else
request.setDestinationInExternalFilesDir(context, directory, fileName);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
long ref = downloadManager.enqueue(request);
}
如果可能,请指导我。 谢谢。