I have already created a directory or a path in my android device using:
File dlFile = new File(Environment.getExternalStorageDirectory()
+ "/RAS/download/files");
And I downloaded a file from my server:
Uri uri = Uri.parse("http://IPADDRESS/RAS/phpword/outputs/template.docx");
downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
How do I put my downloaded file to the directory or path that I have created?
Try this one
DownloadManager.Request request = new DownloadManager.Request(
downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false).setTitle("Data")
.setDescription("Downloading")
.setDestinationInExternalPublicDir("/RAS/download/files", "test.docx");
mgr.enqueue(request);
This is the line where you provide the path
setDestinationInExternalPublicDir("/RAS/download/files", "test.docx");
First, You have to make sure you have the following permissions in your AndroidManifest.xml
file.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
And most importantly, if your target API 23 and the app is running on an Android 6.0+ device, you have to add runtime permission for WRITE_EXTERNAL_STORAGE .
private static final int REQUEST = 111;
if (Build.VERSION.SDK_INT >= 23) {
String[] PERMISSIONS = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (!hasPermissions(mContext, PERMISSIONS)) {
ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST );
} else {
downloadFile();
}
} else {
downloadFile();
}
Get permission result like this.
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
downloadFile();
} else {
Toast.makeText(mContext, "The app was not allowed to write in your storage", Toast.LENGTH_LONG).show();
}
}
}
}
And then you can try like below.
private void downloadFile(){
Uri uri= Uri.parse("http://IPADDRESS/RAS/phpword/outputs/template.docx");
// Create request for android download manager
DownloadManager downloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
// set title and description
request.setTitle("Downloading");
request.setDescription("File download using DownloadManager.");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//set the local destination for download file to a path within the application's external files directory
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"template.docx"); // here file will be download in download directory as template.docs, you can change the directory and file name as you want.
request.setMimeType("*/*");
downloadManager.enqueue(request);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.