Example usage for android.content Context getExternalFilesDir

List of usage examples for android.content Context getExternalFilesDir

Introduction

In this page you can find the example usage for android.content Context getExternalFilesDir.

Prototype

@Nullable
public abstract File getExternalFilesDir(@Nullable String type);

Source Link

Document

Returns the absolute path to the directory on the primary shared/external storage device where the application can place persistent files it owns.

Usage

From source file:Main.java

public static boolean savePreferencesInExternal(Context ctx, SharedPreferences pref) {

    Properties propfile = new Properties();
    Map<String, ?> keymap = pref.getAll();
    Iterator<String> keyit = keymap.keySet().iterator();
    Log.d("External", "Saving prefrences to external Storages");

    while (keyit.hasNext()) {
        String key = keyit.next();
        propfile.put(key, keymap.get(key));
    }/*  w ww.j  a  va  2 s.com*/
    if (isExternalStorageAvailableforWriting() == true) {
        try {
            propfile.storeToXML(new FileOutputStream(new File(ctx.getExternalFilesDir(null), "install_info")),
                    null);
        } catch (Exception e) {

            e.printStackTrace();
        }

        Log.d("External", "Saved prefrences to external ");
        return true;
    } else {
        Log.d("External", "Failed to Save prefrences for external ");
        return false;
    }
}

From source file:org.dkf.jmule.util.SystemUtils.java

/**
 * Use this instead ContextCompat/*  w w  w  .  j  a  v a 2  s .co m*/
 *
 */
public static File[] getExternalFilesDirs(Context context) {
    if (hasKitKatOrNewer()) {
        List<File> dirs = new LinkedList<>();

        for (File f : ContextCompat.getExternalFilesDirs(context, null)) {
            if (f != null) {
                dirs.add(f);
            }
        }

        return dirs.toArray(new File[dirs.size()]);
    } else {
        List<File> dirs = new LinkedList<>();

        dirs.add(context.getExternalFilesDir(null));

        try {
            String secondaryStorages = System.getenv("SECONDARY_STORAGE");
            if (secondaryStorages != null) {
                String[] storages = secondaryStorages.split(File.pathSeparator);
                for (String s : storages) {
                    dirs.add(new File(s));
                }
            }
        } catch (Exception e) {
            LOG.error("Unable to get secondary external storages", e);
        }

        return dirs.toArray(new File[dirs.size()]);
    }
}

From source file:uk.ac.horizon.ubihelper.service.LogManager.java

private static File getLogDirectory(Context context) {
    // may return null
    // Note: needs Write external storage permission
    return context.getExternalFilesDir(FILES_DIR);
}

From source file:fr.shywim.antoinedaniel.utils.Utils.java

public static boolean prepareDownloadFromCdn(Context context, String soundName, String imageName,
        String description, String link, int version, boolean force) throws IOException {
    String extImgPath = context.getExternalFilesDir(null) + "/img/";
    String extSndPath = context.getExternalFilesDir(null) + "/snd/";

    File sndDir = new File(context.getExternalFilesDir(null) + "/snd");
    if (!sndDir.exists())
        if (!sndDir.mkdirs())
            return false;
    File imgDir = new File(context.getExternalFilesDir(null) + "/img");
    if (!imgDir.exists())
        if (!imgDir.mkdirs())
            return false;

    File file = new File(extSndPath + soundName + ".ogg");
    if (!downloadFileFromCdn(context, file, soundName, "snd/", ".ogg", force)) {
        //noinspection ResultOfMethodCallIgnored
        file.delete();// w w w .j a va  2 s . c  om
        return false;
    }

    file = new File(extImgPath + imageName + ".jpg");
    if (!downloadFileFromCdn(context, file, imageName, "img/", ".jpg", force)) {
        //noinspection ResultOfMethodCallIgnored
        file.delete();
        return false;
    }

    Uri uri = Uri.withAppendedPath(ProviderConstants.SOUND_NAME_URI, soundName);
    ContentValues cv = new ContentValues();
    cv.put(ProviderContract.SoundEntry.COLUMN_SOUND_NAME, soundName);
    cv.put(ProviderContract.SoundEntry.COLUMN_IMAGE_NAME, imageName);
    cv.put(ProviderContract.SoundEntry.COLUMN_DESC, description);
    cv.put(ProviderContract.SoundEntry.COLUMN_LINK, link);
    cv.put(ProviderContract.SoundEntry.COLUMN_NAME_VERSION, version);
    if (uri != null) {
        context.getContentResolver().update(uri, cv, null, null);
        return true;
    } else
        return false;
}

From source file:com.bt.download.android.util.SystemUtils.java

/**
 * /*from  w  ww  .  j  av  a2s  .  c  o m*/
 * Use this instead ContextCompat
 * 
 * @param context
 * @return
 */
public static File[] getExternalFilesDirs(Context context) {
    if (hasKitKat()) {
        List<File> dirs = new LinkedList<File>();

        for (File f : ContextCompat.getExternalFilesDirs(context, null)) {
            if (f != null) {
                dirs.add(f);
            }
        }

        return dirs.toArray(new File[0]);
    } else {
        List<File> dirs = new LinkedList<File>();

        dirs.add(context.getExternalFilesDir(null));

        try {
            String secondaryStorages = System.getenv("SECONDARY_STORAGE");
            if (secondaryStorages != null) {
                String[] storages = secondaryStorages.split(File.pathSeparator);
                for (String s : storages) {
                    dirs.add(new File(s));
                }
            }
        } catch (Throwable e) {
            LOG.error("Unable to get secondary external storages", e);
        }

        return dirs.toArray(new File[0]);
    }
}

From source file:most.voip.api.Utils.java

static void copyAssets(Context ctx) {
    AssetManager assetManager = ctx.getAssets();
    String[] files = null;/*from w w w.j a  va2 s.co m*/
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e(TAG, "Failed to get asset file list.", e);
    }
    for (String filename : files) {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(filename);
            File outFile = new File(ctx.getExternalFilesDir(null), filename);
            Log.d(TAG, "Copying asset to " + outFile.getAbsolutePath());
            out = new FileOutputStream(outFile);
            copyFile(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (IOException e) {
            Log.e("tag", "Failed to copy asset file: " + filename, e);
        }
    }
}

From source file:com.cyberocw.habittodosecretary.file.StorageHelper.java

public static boolean deleteExternalStoragePrivateFile(Context mContext, String name, String dir) {

    // Checks for external storage availability
    if (!checkStorage()) {
        //Toast.makeText(mContext, mContext.getString(R.string.storage_not_available), Toast.LENGTH_SHORT).show();
        return false;
    }//from  www  .  j  ava2  s .  c o  m

    File file = new File(mContext.getExternalFilesDir(dir), name);

    Log.d("storage helper", "file absolutepaht=" + file.getAbsolutePath());

    if (file.isFile())
        file.delete();
    else {
        Toast.makeText(mContext, "Not Found file 2", Toast.LENGTH_SHORT).show();
    }

    return true;
}

From source file:com.cyberocw.habittodosecretary.file.StorageHelper.java

public static boolean deleteExternalStoragePrivateFile(Context mContext, String name) {
    boolean res = false;

    // Checks for external storage availability
    if (!checkStorage()) {
        //Toast.makeText(mContext, mContext.getString(R.string.storage_not_available), Toast.LENGTH_SHORT).show();
        return false;
    }/*from  ww w .j av  a 2s.  com*/

    File file = new File(mContext.getExternalFilesDir(null), name);
    if (file.isFile())
        file.delete();
    else {
        //Toast.makeText(mContext, "Not Found file 1", Toast.LENGTH_SHORT).show();
    }

    return true;
}

From source file:it.feio.android.omninotes.utils.StorageHelper.java

public static boolean deleteExternalStoragePrivateFile(Context mContext, String name) {
    boolean res = false;

    // Checks for external storage availability
    if (!checkStorage()) {
        Toast.makeText(mContext, mContext.getString(R.string.storage_not_available), Toast.LENGTH_SHORT).show();
        return false;
    }//from   w  w  w.  j  a  v a 2s. c  om

    File file = new File(mContext.getExternalFilesDir(null), name);
    file.delete();

    return true;
}

From source file:com.example.genshib.mymapproject.AndroidSupportUtil.java

/**
 * Returns if it is required to ask for runtime permission for accessing a directory.
 *
 * @param context   the activity asking/*from   w w  w  .ja va 2  s.  c  om*/
 * @param directory the directory accessed
 * @return true if runtime permission must be asked for
 */
public static boolean runtimePermissionRequiredForReadExternalStorage(Context context, File directory) {
    if (runtimePermissionRequired(context, android.Manifest.permission.READ_EXTERNAL_STORAGE)) {
        try {
            String canonicalPath = directory.getCanonicalPath();
            // not sure if this covers all possibilities: file is freely accessible if it is in the application external cache or external files
            // dir or somewhere else (e.g. internal storage) but not in the general external storage.
            return canonicalPath.startsWith(Environment.getExternalStorageDirectory().getCanonicalPath())
                    && !canonicalPath.startsWith(context.getExternalCacheDir().getCanonicalPath())
                    && !canonicalPath.startsWith(context.getExternalFilesDir(null).getCanonicalPath());
        } catch (IOException e) {
            LOGGER.warning("Directory access exception " + directory.toString() + e.getMessage());
            return true; // ?? it probably means the file cannot be found
        }
    }
    return false;
}