public class DownloadFile extends AsyncTask<Void, Void, Void> {
String str_id, str_docName;
public DownloadFile(String id, String docName) {
str_id = id;
str_docName = docName;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Toast.makeText(mContext, "Downloading..", Toast.LENGTH_SHORT).show();
}
@Override
protected Void doInBackground(Void... voids) {
String url1 = "http://pap.india.com/API/s3_file_download?gid=" + str_id + "&filename=" + str_docName + "&type=icfo";
downloadFile(url1);
return null;
}
}
private void downloadFile(String urlString) {
dManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
if (!urlString.equals("")) {
try {
String fileName = urlString.substring(urlString.lastIndexOf("/") + 1);
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(urlString));
request.setDescription("Download" + fileName + " from "
+ urlString);
request.setTitle("DownloadManager" + file_name);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
final File dir = new File(Environment.getDownloadCacheDirectory(), "sample");
if (!dir.exists()) {
dir.mkdirs();
}
request.setDestinationUri(Uri.parse("file://"
+ Environment.getExternalStorageDirectory()
+ "/sample/" + fileName));
// request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "sample.pdf");
long did = dManager.enqueue(request);
} catch (Exception e) {
e.printStackTrace();
}
}
}
我正在实现下载选项,以便在我的应用程序中下载上传的文件。问题是我能够下载除Pie版本之外的所有移动版本的文件。当我尝试在Pie Mobile版本中下载时文件未下载。< / p>