我正在使用DownloadManager将文件下载到我的Android设备。我可以选择使用以下方式下载到DCIM,下载,Picutures文件夹等,
downloadRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file.jpg");
这很有效。但我只能选择要下载的限制文件夹集。我想使用/ sdcard /
下载到我的SD卡的根目录我试过了:
downloadRequest.setDestinationUri(Uri.fromFile(new File("/sdcard/file.jpg")));
但是这会产生以下异常:
IllegalStateException:android无效的目标组合:4,路径:/sdcard/file.jpg
我该怎么做?我已设置所有必需的权限。
答案 0 :(得分:27)
试试这样。
downloadRequest.setDestinationInExternalPublicDir("/folderName",file.jpg);
这将在您的外部存储根目录中创建一个文件夹,并将file.jpg放入其中。
答案 1 :(得分:2)
使用
修正了它request.setDestinationInExternalPublicDir( "/APP_NAME/", FileNameGetter.getFileName(DATA..url) );
答案 2 :(得分:1)
根据official documentation,您应该只使用以下任一变体,使用其中一个可用的Environment.DIRECTORY_*
常量:
myRequest.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "my_sub_folder/my_file.txt");
目录类型(即第一个参数)可能不为null
,或
myRequest.setDestinationInExternalFilesDir(myContext, null, "my_sub_folder/my_file.txt");
// or
// myRequest.setDestinationInExternalFilesDir(myContext, Environment.DIRECTORY_PICTURES, "my_sub_folder/my_picture.jpg");
目录类型(即第二个参数)可能为null
。
在任何情况下,指定 last 参数中的(子)文件夹都是可选的。