CB-6940: Android: cleanup try/catch exception handling Signed-off-by: Joe Bowser <[email protected]>
Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/commit/44fe0406 Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/tree/44fe0406 Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/diff/44fe0406 Branch: refs/heads/master Commit: 44fe0406a7a99b643bd8c85c84ea4523b0345c84 Parents: 76f869e Author: dzeims <[email protected]> Authored: Mon Jun 16 14:42:27 2014 -0400 Committer: Joe Bowser <[email protected]> Committed: Mon Jun 16 12:43:21 2014 -0700 ---------------------------------------------------------------------- src/android/FileUtils.java | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-file/blob/44fe0406/src/android/FileUtils.java ---------------------------------------------------------------------- diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java index 270855a..db8f4d4 100644 --- a/src/android/FileUtils.java +++ b/src/android/FileUtils.java @@ -129,12 +129,19 @@ public class FileUtils extends CordovaPlugin { HashMap<String, String> availableFileSystems = new HashMap<String,String>(); availableFileSystems.put("files", context.getFilesDir().getAbsolutePath()); - try { availableFileSystems.put("files-external", context.getExternalFilesDir(null).getAbsolutePath()); } catch(Exception e) {} availableFileSystems.put("documents", new File(context.getFilesDir(), "Documents").getAbsolutePath()); - try { availableFileSystems.put("sdcard", Environment.getExternalStorageDirectory().getAbsolutePath()); } catch(Exception e) {} availableFileSystems.put("cache", context.getCacheDir().getAbsolutePath()); - try { availableFileSystems.put("cache-external", context.getExternalCacheDir().getAbsolutePath()); } catch(Exception e) {} availableFileSystems.put("root", "/"); + if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { + try { + availableFileSystems.put("files-external", context.getExternalFilesDir(null).getAbsolutePath()); + availableFileSystems.put("sdcard", Environment.getExternalStorageDirectory().getAbsolutePath()); + availableFileSystems.put("cache-external", context.getExternalCacheDir().getAbsolutePath()); + } + catch(IOException e) { + /* If external storage is unavailable, context.getExternal* returns null */ + } + } return availableFileSystems; } @@ -863,10 +870,17 @@ public class FileUtils extends CordovaPlugin { ret.put("applicationStorageDirectory", toDirUrl(context.getFilesDir().getParentFile())); ret.put("dataDirectory", toDirUrl(context.getFilesDir())); ret.put("cacheDirectory", toDirUrl(context.getCacheDir())); - try { ret.put("externalApplicationStorageDirectory", toDirUrl(context.getExternalFilesDir(null).getParentFile())); } catch(Exception e) {} - try { ret.put("externalDataDirectory", toDirUrl(context.getExternalFilesDir(null))); } catch(Exception e) {} - try { ret.put("externalCacheDirectory", toDirUrl(context.getExternalCacheDir())); } catch(Exception e) {} - try { ret.put("externalRootDirectory", toDirUrl(Environment.getExternalStorageDirectory())); } catch(Exception e) {} + if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { + try { + ret.put("externalApplicationStorageDirectory", toDirUrl(context.getExternalFilesDir(null).getParentFile())); + ret.put("externalDataDirectory", toDirUrl(context.getExternalFilesDir(null))); + ret.put("externalCacheDirectory", toDirUrl(context.getExternalCacheDir())); + ret.put("externalRootDirectory", toDirUrl(Environment.getExternalStorageDirectory())); + } + catch(IOException e) { + /* If external storage is unavailable, context.getExternal* returns null */ + } + } return ret; }
