I have already read many similar questions, but I could not find a solution to the problem. At first glance, I'm doing everything right, but the directory creation does not work for API 17 and low.
Manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
...
Cod
// скачиваем файл
public void downloadWallpaper(String url, String name) {
final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
SharedPreferences preferenceManager = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
String Download_ID = "PREF_DOWNLOAD_ID";
// проверяем наличие подключения
if (activeNetwork != null && activeNetwork.isConnected()) {
for (int z = 0; z < 1; z++) {
// проверяем был ли уже загружен файл
File file = new File(Environment.getExternalStorageDirectory() + "/LiveWallpapers/" + name);
if (!file.exists()) {
// загружаем файл, т.к. его нет
Uri downloadUri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
// назначаем имя для файла
request.setDestinationInExternalPublicDir("LiveWallpapers", name);
// сохраняем request id
SharedPreferences.Editor PrefEdit = preferenceManager.edit();
long id = downloadManager.enqueue(request);
PrefEdit.putLong(Download_ID, id);
PrefEdit.apply();
} else {
// такой файл уже есть
break;
}
for (int p = 0; p < 30; p++) {
// проверяем был ли уже загружен файл
if (!file.exists()) {
// ожидаем загрузки файла
try {Thread.sleep(1000);} catch (InterruptedException e) {}
} else {
// файл загрузился
try {Thread.sleep(1000);} catch (InterruptedException e) {}
break;
}
}
}
}
}
For API 18 and high its work (/storage/sdcard/LiveWallpapers/...)
For API 17 and low its not work (/mnt/sdcard/LiveWallpapers/...)
I also tried to create a directory manually
File folder = new File(Environment.getExternalStorageDirectory() + "/LiveWallpapers");
if (!folder.exists()) {
folder.mkdirs();
}
if (folder.exists()) {
...
}
The application downloads a file from the Internet and creates a directory LiveWallpapers. For API 14, 15, 16, 17 can not create directory.
LogCat
03-15 15:41:15.411 2059-2059/com.developer.skyline.livewallpapers E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Unable to create directory: /mnt/sdcard/LiveWallpapers
at android.app.DownloadManager$Request.setDestinationInExternalPublicDir(DownloadManager.java:496)
at com.developer.skyline.livewallpapers.LiveWallpaperService$GifEngine.downloadWallpaper(LiveWallpaperService.java:379)
at com.developer.skyline.livewallpapers.LiveWallpaperService$GifEngine.<init>(LiveWallpaperService.java:206)
at com.developer.skyline.livewallpapers.LiveWallpaperService.onCreateEngine(LiveWallpaperService.java:41)
at android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:1012)
at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:61)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)