我使用DownloadManager类在android中下载文件
这是我的下载代码:
Uri downloadUri = Uri.parse(urlString);
DownloadManager.Request request = new
DownloadManager.Request(downloadUri);
request.setDescription(des).setTitle(titleAudio).setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED).
setDestinationInExternalPublicDir(
"/my file name/" , titleAudio + String.valueOf(colForUrl) + pasvand);
long id = downloadManager.enqueue(request);
SharedPreferences.Editor prefEdit = preferenceManager.edit();
prefEdit.putLong(strPref_Download_ID, id);
prefEdit.commit();
但当我在某些设备(三星Galaxy S5)中运行应用程序时,我会收到以下错误:
java.lang.IllegalStateException: Unable to create directory: /storage/emulated/0/my file name
这是在setDestinationInExternalPublicDir(..)
中引起的
但在Nexus 7中,每件事都是正确的,我没有任何问题
那么,我错在哪里?!
根据文档:
setDestinationInExternalPublicDir(String dirType, String subPath)
- dirType-要传递给getExternalStoragePublicDirectory的目录类型(字符串)
- subPath-外部目录中的路径,包括目标文件名
dirType应该是DIRECTORY_MUSIC、DIRECTORY_PODCASTS、DIRECTOR,DIRECTORY_铃声、DIRECTORY_alands、DIRECTORY_NOTIFICATIONS,DIRECTORY_PICTURES、DIRECTORY_MOVIES、DIRECTOR Y_DOWNLOADS或DIRECTORY_DCIM。不能为null。
因此,例如,尝试:
setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC, titleAudio + String.valueOf(colForUrl) + pasvand);
如果它不是你想要的文件夹,就从那里开始。