More detailed logs in case getFilesDir fails
This commit is contained in:
parent
147f010d1b
commit
132ce36a57
@ -55,6 +55,7 @@ import org.isoron.uhabits.BaseActivity;
|
||||
import org.isoron.uhabits.commands.Command;
|
||||
import org.isoron.uhabits.commands.ToggleRepetitionCommand;
|
||||
import org.isoron.uhabits.dialogs.FilePickerDialog;
|
||||
import org.isoron.uhabits.helpers.DatabaseHelper;
|
||||
import org.isoron.uhabits.helpers.DateHelper;
|
||||
import org.isoron.uhabits.helpers.UIHelper.OnSavedListener;
|
||||
import org.isoron.uhabits.helpers.HintManager;
|
||||
@ -428,8 +429,12 @@ public class ListHabitsFragment extends Fragment
|
||||
|
||||
public void showImportDialog()
|
||||
{
|
||||
File dir = activity.getExternalFilesDir(null);
|
||||
if(dir == null) return;
|
||||
File dir = DatabaseHelper.getFilesDir(null);
|
||||
if(dir == null)
|
||||
{
|
||||
activity.showToast(R.string.could_not_import);
|
||||
return;
|
||||
}
|
||||
|
||||
FilePickerDialog picker = new FilePickerDialog(activity, dir);
|
||||
picker.setListener(new FilePickerDialog.OnFileSelectedListener()
|
||||
|
||||
@ -24,6 +24,7 @@ import android.database.Cursor;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.util.Log;
|
||||
|
||||
import com.activeandroid.ActiveAndroid;
|
||||
import com.activeandroid.Cache;
|
||||
@ -126,14 +127,24 @@ public class DatabaseHelper
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static File getFilesDir(String prefix)
|
||||
public static File getFilesDir(@Nullable String prefix)
|
||||
{
|
||||
if(prefix == null) prefix = "";
|
||||
|
||||
Context context = HabitsApplication.getContext();
|
||||
if(context == null) return null;
|
||||
if(context == null)
|
||||
{
|
||||
Log.e("DatabaseHelper", "getFilesDir: no application context available");
|
||||
return null;
|
||||
}
|
||||
|
||||
File chosenDir = null;
|
||||
File externalFilesDirs[] = ContextCompat.getExternalFilesDirs(context, null);
|
||||
if(externalFilesDirs == null) return null;
|
||||
if(externalFilesDirs == null)
|
||||
{
|
||||
Log.e("DatabaseHelper", "getFilesDir: getExternalFilesDirs returned null");
|
||||
return null;
|
||||
}
|
||||
|
||||
for(File dir : externalFilesDirs)
|
||||
{
|
||||
@ -142,10 +153,18 @@ public class DatabaseHelper
|
||||
break;
|
||||
}
|
||||
|
||||
if(chosenDir == null) return null;
|
||||
if(chosenDir == null)
|
||||
{
|
||||
Log.e("DatabaseHelper", "getFilesDir: all external dirs are null or non-writable");
|
||||
return null;
|
||||
}
|
||||
|
||||
File dir = new File(String.format("%s/%s/", chosenDir.getAbsolutePath(), prefix));
|
||||
dir.mkdirs();
|
||||
if (!dir.exists() && !dir.mkdirs())
|
||||
{
|
||||
Log.e("DatabaseHelper", "getFilesDir: chosen dir does not exist and cannot be created");
|
||||
return null;
|
||||
}
|
||||
|
||||
return dir;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user