问题
I use DownloadManager class to download a file in android
and this is my code for download:
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();
but when I run app in some device(Samsung Galaxy S5) I get this error:
java.lang.IllegalStateException: Unable to create directory: /storage/emulated/0/my file name
and this is caused in setDestinationInExternalPublicDir(..)
but in Nexus 7 every thing is right , I have not any problem!!
So, Where is my wrong?!
回答1:
According to the docs:
setDestinationInExternalPublicDir(String dirType, String subPath)
- dirType - the directory type to pass to getExternalStoragePublicDirectory(String)
- subPath - the path within the external directory, including the destination filename
dirType Should be one of DIRECTORY_MUSIC, DIRECTORY_PODCASTS, DIRECTORY_RINGTONES, DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS, DIRECTORY_PICTURES, DIRECTORY_MOVIES, DIRECTORY_DOWNLOADS, or DIRECTORY_DCIM. May not be null.
So, for example, try:
setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC, titleAudio + String.valueOf(colForUrl) + pasvand);
And go from there if it's not exactly the folder that you want.
回答2:
Android API level 23 has a new concept of permission model. The concept suggests to check and ask users for permission in runtime.
System permissions are divided into two categories, normal and dangerous:
1- Normal permissions do not directly risk the user's privacy. If your app lists a normal permission in its manifest, the system grants the permission automatically.
2- Dangerous permissions can give the app access to the user's confidential data. If your app lists a normal permission in its manifest, the system grants the permission automatically. If you list a dangerous permission, the user has to explicitly give approval to your app.
WRITE_EXTERNAL_STORAGE falls under dangerous permission, therefore you have to check in runtime to have permission to create a directory.
The first step is to add permissin request in Manifest.xml by adding the following:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
The above permission will not allow the app to write into external storage from Android 6.0, therefore it is required to ask user in runtime for permission.
Check for permission:
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
Permission Request Method:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
Visit Requesting Permissions at Run Time for more information and required code.
来源:https://stackoverflow.com/questions/26802707/why-get-java-lang-illegalstateexception-unable-to-create-directory-error