1

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)
user233804
  • 53
  • 1
  • 8
  • `already read many similar questions, but I could not find a solution to the problem`/ I see no question. And you did not describe a problem. Can you please write a normal post? – greenapps Mar 15 '17 at 15:24
  • Thanks. I edited the first message. – user233804 Mar 15 '17 at 15:44
  • `DownloadManager.Request setDestinationInExternalPublicDir(String dirType, String subPath)`. You have to supply two strings as parameters. Well you do but not in the right way. – greenapps Mar 15 '17 at 15:58
  • 1
    You might want to check the state of External Storage using `getExternalStorageState()` to verify if the external storage exists, and is accessible – developeralways Mar 15 '17 at 15:58
  • if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) Log.i(TAG, "False"); The log "False" was displayed – user233804 Mar 15 '17 at 17:56
  • So, clearly the External Storage is not mounted and that is the reason why it is not able to create a directory in that location. You need to change the location of the file or have a mounted sdcard in the phone/emulator. – developeralways Mar 15 '17 at 18:07
  • Checked on a real device, everything works. The directory was created, the file was downloaded. The problem with the emulator in Android Studio. – user233804 Mar 16 '17 at 04:58

0 Answers0