當前位置: 首頁>>代碼示例>>Java>>正文


Java ContextCompat.getExternalFilesDirs方法代碼示例

本文整理匯總了Java中android.support.v4.content.ContextCompat.getExternalFilesDirs方法的典型用法代碼示例。如果您正苦於以下問題:Java ContextCompat.getExternalFilesDirs方法的具體用法?Java ContextCompat.getExternalFilesDirs怎麽用?Java ContextCompat.getExternalFilesDirs使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在android.support.v4.content.ContextCompat的用法示例。


在下文中一共展示了ContextCompat.getExternalFilesDirs方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getExternalFilesDir

import android.support.v4.content.ContextCompat; //導入方法依賴的package包/類
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;
    }
}
 
開發者ID:AbyxBelgium,項目名稱:Loyalty,代碼行數:24,代碼來源:FileManager.java

示例2: getRootDirPath

import android.support.v4.content.ContextCompat; //導入方法依賴的package包/類
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();
    }
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:9,代碼來源:Utils.java

示例3: getRootDirPath

import android.support.v4.content.ContextCompat; //導入方法依賴的package包/類
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();
    }
}
 
開發者ID:MindorksOpenSource,項目名稱:PRDownloader,代碼行數:10,代碼來源:Utils.java

示例4: getVer4_4DownloadPath

import android.support.v4.content.ContextCompat; //導入方法依賴的package包/類
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();
}
 
開發者ID:JackChan1999,項目名稱:letv,代碼行數:15,代碼來源:StoreUtils.java

示例5: getFilePath

import android.support.v4.content.ContextCompat; //導入方法依賴的package包/類
/**
 * 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;
}
 
開發者ID:Esri,項目名稱:mapbook-android,代碼行數:20,代碼來源:CredentialCryptographer.java

示例6: providesStorageDirectory

import android.support.v4.content.ContextCompat; //導入方法依賴的package包/類
@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;
}
 
開發者ID:Esri,項目名稱:mapbook-android,代碼行數:14,代碼來源:MapbookModule.java

示例7: getAllStorageLocations

import android.support.v4.content.ContextCompat; //導入方法依賴的package包/類
/**
 * @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);
  }
}
 
開發者ID:Elias33,項目名稱:Quran,代碼行數:59,代碼來源:StorageUtils.java

示例8: getAllStorageLocations

import android.support.v4.content.ContextCompat; //導入方法依賴的package包/類
/**
 * @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);
    }
}
 
開發者ID:fekracomputers,項目名稱:IslamicLibraryAndroid,代碼行數:59,代碼來源:StorageUtils.java


注:本文中的android.support.v4.content.ContextCompat.getExternalFilesDirs方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。