Java Code Examples for android.support.v4.content.ContextCompat.getExternalFilesDirs()

The following are Jave code examples for showing how to use getExternalFilesDirs() of the android.support.v4.content.ContextCompat class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: Loyalty   File: FileManager.java   Source Code and License Vote up 6 votes
public File getExternalFilesDir() throws IOException {
    File tempFile = null;

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        String state = Environment.getExternalStorageState();

        if (Environment.MEDIA_MOUNTED.equals(state)) {
            // compatible for ALL the versions
            File[] dirs = ContextCompat.getExternalFilesDirs(context, null); //null, no specific sub directory
            if (dirs.length > 0) {
                tempFile  = dirs[dirs.length -1];
            }
        }
    } else {
        tempFile = Environment.getExternalStorageDirectory();
    }

    if (tempFile == null) {
        throw new IOException("External file could not be initialized!");
    } else {
        return tempFile;
    }
}
 
Example 2
Project: GitHub   File: Utils.java   Source Code and License Vote up 5 votes
public static String getRootDirPath(Context context) {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        File file = ContextCompat.getExternalFilesDirs(context.getApplicationContext(), null)[0];
        return file.getAbsolutePath();
    } else {
        return context.getApplicationContext().getFilesDir().getAbsolutePath();
    }
}
 
Example 3
Project: GitHub   File: Utils.java   Source Code and License Vote up 5 votes
public static String getRootDirPath(Context context) {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        File file = ContextCompat.getExternalFilesDirs(context.getApplicationContext(), null)[0];
        return file.getAbsolutePath();
    } else {
        return context.getApplicationContext().getFilesDir().getAbsolutePath();
    }
}
 
Example 4
Project: GitHub   File: Utils.java   Source Code and License Vote up 5 votes
public static String getRootDirPath(Context context) {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        File file = ContextCompat.getExternalFilesDirs(context.getApplicationContext(), null)[0];
        return file.getAbsolutePath();
    } else {
        return context.getApplicationContext().getFilesDir().getAbsolutePath();
    }
}
 
Example 5
Project: GitHub   File: Utils.java   Source Code and License Vote up 5 votes
public static String getRootDirPath(Context context) {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        File file = ContextCompat.getExternalFilesDirs(context.getApplicationContext(), null)[0];
        return file.getAbsolutePath();
    } else {
        return context.getApplicationContext().getFilesDir().getAbsolutePath();
    }
}
 
Example 6
Project: GitHub   File: Utils.java   Source Code and License Vote up 5 votes
public static String getRootDirPath(Context context) {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        File file = ContextCompat.getExternalFilesDirs(context.getApplicationContext(), null)[0];
        return file.getAbsolutePath();
    } else {
        return context.getApplicationContext().getFilesDir().getAbsolutePath();
    }
}
 
Example 7
Project: GitHub   File: Utils.java   Source Code and License Vote up 5 votes
public static String getRootDirPath(Context context) {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        File file = ContextCompat.getExternalFilesDirs(context.getApplicationContext(), null)[0];
        return file.getAbsolutePath();
    } else {
        return context.getApplicationContext().getFilesDir().getAbsolutePath();
    }
}
 
Example 8
Project: PRDownloader   File: Utils.java   Source Code and License Vote up 5 votes
public static String getRootDirPath(Context context) {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        File file = ContextCompat.getExternalFilesDirs(context.getApplicationContext(),
                null)[0];
        return file.getAbsolutePath();
    } else {
        return context.getApplicationContext().getFilesDir().getAbsolutePath();
    }
}
 
Example 9
Project: letv   File: StoreUtils.java   Source Code and License Vote up 5 votes
public static String getVer4_4DownloadPath(Context context) {
    File[] dirs = ContextCompat.getExternalFilesDirs(context, null);
    LogInfo.log("ljnalex", "dirs.length:" + dirs.length);
    if (dirs == null || dirs.length == 0) {
        return "";
    }
    if (isSdcardAvailable() && dirs.length >= 2 && dirs[1] != null) {
        return dirs[1].getAbsolutePath();
    }
    if (isSdcardAvailable() || dirs[0] == null) {
        return "";
    }
    return dirs[0].getAbsolutePath();
}
 
Example 10
Project: mapbook-android   File: CredentialCryptographer.java   Source Code and License Vote up 5 votes
/**
 * Return the absolute file path for a file in the app directory
 * @param fileName - String representing file name
 * @return String representing the absolute path to the file
 */
private String getFilePath(final String fileName){
  String filepath = null;
  final File [] dirFiles = ContextCompat.getExternalFilesDirs(mContext, null);

  // We don't encrypt files if we can't store them...
  if (dirFiles.length == 0){
    Log.i(TAG, "Data cannot be encrypted because no app directories were found.");
    return filepath;
  }else{
    final File f = dirFiles[0];
    filepath = f.getAbsolutePath() + File.separator + fileName;
  }
  return filepath;
}
 
Example 11
Project: mapbook-android   File: MapbookModule.java   Source Code and License Vote up 5 votes
@Provides
@MapbookApplicationScope
@Named("storageDirectory")
public File providesStorageDirectory(final Context context) {
  File directory = null;
  final File [] files = ContextCompat.getExternalFilesDirs(context, null);
  if (files.length > 0){
    directory = files[0];
  }else{
    directory = Environment.getDataDirectory();
  }
  return directory;
}
 
Example 12
Project: Quran   File: StorageUtils.java   Source Code and License Vote up 4 votes
/**
 * @return A List of all storage locations available
 */
public static List<Storage> getAllStorageLocations(Context context) {

  /*
    This first condition is the code moving forward, since the else case is a bunch
    of unsupported hacks.

    For Kitkat and above, we rely on Environment.getExternalFilesDirs to give us a list
    of application writable directories (none of which require WRITE_EXTERNAL_STORAGE on
    Kitkat and above).

    Previously, we only would show anything if there were at least 2 entries. For M,
    some changes were made, such that on M, we even show this if there is only one
    entry.

    Irrespective of whether we require 1 entry (M) or 2 (Kitkat and L), we add an
    additional entry explicitly for the sdcard itself, (the one requiring
    WRITE_EXTERNAL_STORAGE to write).

    Thus, on Kitkat, the user may either:
    a. not see any item (if there's only one entry returned by getExternalFilesDirs, we won't
    show any options since it's the same sdcard and we have the permission and the user can't
    revoke it pre-Kitkat), or
    b. see 3+ items - /sdcard, and then at least 2 external fiels directories.

    on M, the user will always see at least 2 items (the external files dir and the actual
    external storage directory), and potentially more (depending on how many items are returned
    by getExternalFilesDirs).
   */
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    List<Storage> result = new ArrayList<>();
    int limit = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? 1 : 2;
    final File[] mountPoints = ContextCompat.getExternalFilesDirs(context, null);
    if (mountPoints != null && mountPoints.length >= limit) {
      int typeId;
      if (!Environment.isExternalStorageRemovable() || Environment.isExternalStorageEmulated()) {
        typeId = R.string.prefs_sdcard_internal;
      } else {
        typeId = R.string.prefs_sdcard_external;
      }

      int number = 1;
      result.add(new Storage(context.getString(typeId, number),
          Environment.getExternalStorageDirectory().getAbsolutePath(),
          Build.VERSION.SDK_INT >= Build.VERSION_CODES.M));
      for (File mountPoint : mountPoints) {
        result.add(new Storage(context.getString(typeId, number++),
            mountPoint.getAbsolutePath()));
        typeId = R.string.prefs_sdcard_external;
      }
    }
    return result;
  } else {
    return getLegacyStorageLocations(context);
  }
}
 
Example 13
Project: IslamicLibraryAndroid   File: StorageUtils.java   Source Code and License Vote up 4 votes
/**
 * @return A List of all storage locations available
 */
public static List<Storage> getAllStorageLocations(Context context) {

/*
  This first condition is the code moving forward, since the else case is a bunch
  of unsupported hacks.

  For Kitkat and above, we rely on Environment.getExternalFilesDirs to give us a list
  of application writable directories (none of which require WRITE_EXTERNAL_STORAGE on
  Kitkat and above).

  Previously, we only would show anything if there were at least 2 entries. For M,
  some changes were made, such that on M, we even show this if there is only one
  entry.

  Irrespective of whether we require 1 entry (M) or 2 (Kitkat and L), we add an
  additional entry explicitly for the sdcard itself, (the one requiring
  WRITE_EXTERNAL_STORAGE to write).

  Thus, on Kitkat, the user may either:
  a. not see any item (if there's only one entry returned by getExternalFilesDirs, we won't
  show any options since it's the same sdcard and we have the permission and the user can't
  revoke it pre-Kitkat), or
  b. see 3+ items - /sdcard, and then at least 2 external fiels directories.

  on M, the user will always see at least 2 items (the external files dir and the actual
  external storage directory), and potentially more (depending on how many items are returned
  by getExternalFilesDirs).
 */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        List<Storage> result = new ArrayList<>();
        int limit = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? 1 : 2;
        final File[] mountPoints = ContextCompat.getExternalFilesDirs(context, null);
        if (mountPoints != null && mountPoints.length >= limit) {
            int typeId;
            if (!Environment.isExternalStorageRemovable() || Environment.isExternalStorageEmulated()) {
                typeId = R.string.prefs_sdcard_internal;
            } else {
                typeId = R.string.prefs_sdcard_external;
            }

            int number = 1;
            result.add(new Storage(context.getString(typeId, number),
                    Environment.getExternalStorageDirectory().getAbsolutePath(),
                    Build.VERSION.SDK_INT >= Build.VERSION_CODES.M));
            for (File mountPoint : mountPoints) {
                result.add(new Storage(context.getString(typeId, number++),
                        mountPoint.getAbsolutePath()));
                typeId = R.string.prefs_sdcard_external;
            }
        }
        return result;
    } else {
        return getLegacyStorageLocations(context);
    }
}