From 99122ccc0309000b0e78ad5b8181a51173924bc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacek=20Musia=C5=82?= Date: Wed, 23 Dec 2015 17:52:01 +0100 Subject: [PATCH] added check if directory exist and try create it Also I added new variable `'final File dir`' with value of key "download_path_preference" or externalStorageDirectory. Firstly I check if dir exits, then eventually try to create it and next - download file. --- .../main/java/org/schabi/newpipe/DownloadDialog.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/src/main/java/org/schabi/newpipe/DownloadDialog.java b/app/src/main/java/org/schabi/newpipe/DownloadDialog.java index fa79e5f82..fe4ea7b4b 100644 --- a/app/src/main/java/org/schabi/newpipe/DownloadDialog.java +++ b/app/src/main/java/org/schabi/newpipe/DownloadDialog.java @@ -7,6 +7,7 @@ import android.content.DialogInterface; import android.content.SharedPreferences; import android.net.Uri; import android.os.Bundle; +import android.os.Environment; import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; @@ -72,6 +73,17 @@ public class DownloadDialog extends DialogFragment { default: Log.d(TAG, "lolz"); } + //to avoid hard-coded string like "/storage/emulated/0/NewPipe" + final File dir = new File(defaultPreferences.getString( + "download_path_preference", + Environment.getExternalStorageDirectory().getAbsolutePath() + "/NewPipe")); + if(!dir.exists()) { + boolean mkdir = dir.mkdir(); //attempt to create directory + if(!mkdir && !dir.isDirectory()) { + Log.e(TAG, "Cant' create directory named " + dir.toString()); + //TODO notify user "download directory should be changed" ? + } + } DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); DownloadManager.Request request = new DownloadManager.Request( Uri.parse(url));