android setDestinationInExternalPublicDir 下载到SD卡根目录

时间:2023-11-11 08:31:50

一:setDestinationInExternalPublicDir(“Trinea”, “MeiLiShuo.apk”);表示设置下载地址为sd卡的Trinea文件夹,文件名为MeiLiShuo.apk。
设置下载路径接口为setDestinationUri,setDestinationInExternalFilesDir,setDestinationToSystemCache。其中setDestinationToSystemCache仅限系统app使用。

二:DownloadManager下载到内置目录用这个setDestinationInExternalFilesDir(Context,null,filename)

SD卡根目录下创建文件夹

     /**
* 取得程式指定SDCard文件下载目录
* 内置sdCard
* APP公用目录
*/
public static String getCommonPath() {
//有sd卡
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
// 创建一个文件夹对象,赋值为外部存储器的目录
File sdcardDir = Environment.getExternalStorageDirectory();
// 得到一个路径,内容是sdcard的文件夹路径和名字
String path = sdcardDir.getPath() + "/" + "test";
File path1 = new File(path);
if (!path1.exists())
// 若不存在,创建目录,可以在应用启动的时候创建
path1.mkdirs(); return path;
} else{
//无SD卡
return "";
} }