Java 如何在创建的目录中下载文件
我已经在我的android设备中使用以下工具创建了一个目录或路径:Java 如何在创建的目录中下载文件,java,android,Java,Android,我已经在我的android设备中使用以下工具创建了一个目录或路径: File dlFile = new File(Environment.getExternalStorageDirectory() + "/RAS/download/files"); 我从服务器上下载了一个文件: Uri uri = Uri.parse("http://IPADDRESS/RAS/phpword/outputs/template.docx"); downloadManager =
File dlFile = new File(Environment.getExternalStorageDirectory()
+ "/RAS/download/files");
我从服务器上下载了一个文件:
Uri uri = Uri.parse("http://IPADDRESS/RAS/phpword/outputs/template.docx");
downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
如何将下载的文件放入我创建的目录或路径?试试这个
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);
这是您提供路径的行
setDestinationInExternalPublicDir("/RAS/download/files", "test.docx");
首先,您必须确保在
AndroidManifest.xml
文件中具有以下权限
<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" />
获得这样的权限结果
@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();
}
}
}
}
然后你可以尝试下面的方法
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);
}
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);
}