繁体   English   中英

如何在创建的目录中下载文件

[英]How to download file in the directory created

我已经使用以下命令在我的 android 设备中创建了一个目录或路径:

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" />

最重要的是,如果您的目标 API 23和应用程序在Android 6.0+设备上运行,您必须为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();
}

得到这样的许可结果。

@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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM