Java Code Examples for androidx.core.content.ContextCompat#getExternalFilesDirs()

The following examples show how to use androidx.core.content.ContextCompat#getExternalFilesDirs() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileUtils.java    From a with GNU General Public License v3.0 6 votes vote down vote up
public static ArrayList<String> getExtSdCardPaths(Context con) {
    ArrayList<String> paths = new ArrayList<String>();
    File[] files = ContextCompat.getExternalFilesDirs(con, "external");
    File firstFile = files[0];
    for (File file : files) {
        if (file != null && !file.equals(firstFile)) {
            int index = file.getAbsolutePath().lastIndexOf("/Android/data");
            if (index < 0) {
                Log.w("", "Unexpected external file dir: " + file.getAbsolutePath());
            } else {
                String path = file.getAbsolutePath().substring(0, index);
                try {
                    path = new File(path).getCanonicalPath();
                } catch (IOException e) {
                    // Keep non-canonical path.
                }
                paths.add(path);
            }
        }
    }
    return paths;
}
 
Example 2
Source File: FileUtils.java    From MyBookshelf with GNU General Public License v3.0 6 votes vote down vote up
public static ArrayList<String> getExtSdCardPaths(Context con) {
    ArrayList<String> paths = new ArrayList<String>();
    File[] files = ContextCompat.getExternalFilesDirs(con, "external");
    File firstFile = files[0];
    for (File file : files) {
        if (file != null && !file.equals(firstFile)) {
            int index = file.getAbsolutePath().lastIndexOf("/Android/data");
            if (index < 0) {
                Log.w("", "Unexpected external file dir: " + file.getAbsolutePath());
            } else {
                String path = file.getAbsolutePath().substring(0, index);
                try {
                    path = new File(path).getCanonicalPath();
                } catch (IOException e) {
                    // Keep non-canonical path.
                }
                paths.add(path);
            }
        }
    }
    return paths;
}
 
Example 3
Source File: FileUtil.java    From HaoReader with GNU General Public License v3.0 6 votes vote down vote up
public static ArrayList<String> getExtSdCardPaths(Context con) {
    ArrayList<String> paths = new ArrayList<String>();
    File[] files = ContextCompat.getExternalFilesDirs(con, "external");
    File firstFile = files[0];
    for (File file : files) {
        if (file != null && !file.equals(firstFile)) {
            int index = file.getAbsolutePath().lastIndexOf("/Android/data");
            if (index < 0) {
                Log.w("", "Unexpected external file dir: " + file.getAbsolutePath());
            } else {
                String path = file.getAbsolutePath().substring(0, index);
                try {
                    path = new File(path).getCanonicalPath();
                } catch (IOException e) {
                    // Keep non-canonical path.
                }
                paths.add(path);
            }
        }
    }
    return paths;
}
 
Example 4
Source File: StorageHelper.java    From android-app with GNU General Public License v3.0 6 votes vote down vote up
public static String getExternalStoragePath() {
    if(externalStoragePath == null) {
        String returnPath = null;
        File[] externalFilesDirs = ContextCompat.getExternalFilesDirs(App.getInstance(), null);
        // TODO: better SD Card detection
        for(File extStorageDir: externalFilesDirs) {
            if(extStorageDir == null) {
                Log.w(TAG, "getExternalStoragePath() extStorageDir is null");
                continue;
            }

            returnPath = extStorageDir.getPath();
            Log.d(TAG, "getExternalStoragePath() extStorageDir.getPath(): " + returnPath);
            break;
        }

        Log.d(TAG, "getExternalStoragePath() returnPath: " + returnPath);
        return externalStoragePath = returnPath;
    }

    return externalStoragePath;
}
 
Example 5
Source File: Util.java    From FridaLoader with MIT License 5 votes vote down vote up
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
Source File: FileExplorerHelper.java    From odyssey with GNU General Public License v3.0 5 votes vote down vote up
/**
 * return the list of available storage volumes
 */
public List<String> getStorageVolumes(Context context) {
    // create storage volume list
    ArrayList<String> storagePathList = new ArrayList<>();

    File[] storageList = ContextCompat.getExternalFilesDirs(context, null);
    for (File storageFile : storageList) {
        if (null != storageFile) {
            storagePathList.add(storageFile.getAbsolutePath().replaceAll("/Android/data/" + context.getPackageName() + "/files", ""));
        }
    }
    storagePathList.add("/");

    return storagePathList;
}
 
Example 7
Source File: FileProvider.java    From MHViewer with Apache License 2.0 4 votes vote down vote up
/**
 * Parse and return {@link PathStrategy} for given authority as defined in
 * {@link #META_DATA_FILE_PROVIDER_PATHS} {@code <meta-data>}.
 *
 * @see #getPathStrategy(Context, String)
 */
private static PathStrategy parsePathStrategy(Context context, String authority)
        throws IOException, XmlPullParserException {
    final SimplePathStrategy strat = new SimplePathStrategy(authority);

    final ProviderInfo info = context.getPackageManager()
            .resolveContentProvider(authority, PackageManager.GET_META_DATA);
    final XmlResourceParser in = info.loadXmlMetaData(
            context.getPackageManager(), META_DATA_FILE_PROVIDER_PATHS);
    if (in == null) {
        throw new IllegalArgumentException(
                "Missing " + META_DATA_FILE_PROVIDER_PATHS + " meta-data");
    }

    int type;
    while ((type = in.next()) != END_DOCUMENT) {
        if (type == START_TAG) {
            final String tag = in.getName();

            final String name = in.getAttributeValue(null, ATTR_NAME);
            String path = in.getAttributeValue(null, ATTR_PATH);

            File target = null;
            if (TAG_ROOT_PATH.equals(tag)) {
                target = DEVICE_ROOT;
            } else if (TAG_FILES_PATH.equals(tag)) {
                target = context.getFilesDir();
            } else if (TAG_CACHE_PATH.equals(tag)) {
                target = context.getCacheDir();
            } else if (TAG_EXTERNAL.equals(tag)) {
                target = Environment.getExternalStorageDirectory();
            } else if (TAG_EXTERNAL_FILES.equals(tag)) {
                File[] externalFilesDirs = ContextCompat.getExternalFilesDirs(context, null);
                if (externalFilesDirs.length > 0) {
                    target = externalFilesDirs[0];
                }
            } else if (TAG_EXTERNAL_CACHE.equals(tag)) {
                File[] externalCacheDirs = ContextCompat.getExternalCacheDirs(context);
                if (externalCacheDirs.length > 0) {
                    target = externalCacheDirs[0];
                }
            }

            if (target != null) {
                strat.addRoot(name, buildPath(target, path));
            }
        }
    }

    return strat;
}
 
Example 8
Source File: LocalFileListFragment.java    From Kore with Apache License 2.0 4 votes vote down vote up
@Override
protected TabsAdapter createTabsAdapter(DataHolder dataHolder) {
    ListType.Sort sortMethod = new ListType.Sort(ListType.Sort.SORT_METHOD_PATH, true, true);

    Bundle dcimFileListArgs = new Bundle();
    String dcim = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
    dcimFileListArgs.putString(LocalMediaFileListFragment.ROOT_PATH_LOCATION, dcim);
    dcimFileListArgs.putParcelable(LocalMediaFileListFragment.SORT_METHOD, sortMethod);

    Bundle directoryMusicFileListArgs = new Bundle();
    String directoryMusic = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath();
    directoryMusicFileListArgs.putString(LocalMediaFileListFragment.ROOT_PATH_LOCATION, directoryMusic);
    directoryMusicFileListArgs.putParcelable(LocalMediaFileListFragment.SORT_METHOD, sortMethod);

    Bundle directoryMoviesFileListArgs = new Bundle();
    String directoryMovies = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath();
    directoryMoviesFileListArgs.putString(LocalMediaFileListFragment.ROOT_PATH_LOCATION, directoryMovies);
    directoryMoviesFileListArgs.putParcelable(LocalMediaFileListFragment.SORT_METHOD, sortMethod);

    Bundle externalStorageFileListArgs = new Bundle();
    String externalStorage = Environment.getExternalStorageDirectory().getAbsolutePath();
    externalStorageFileListArgs.putString(LocalMediaFileListFragment.ROOT_PATH_LOCATION, externalStorage);
    externalStorageFileListArgs.putParcelable(LocalMediaFileListFragment.SORT_METHOD, sortMethod);

    TabsAdapter tabsAdapter = new TabsAdapter(getActivity(), getChildFragmentManager())
            .addTab(LocalMediaFileListFragment.class, dcimFileListArgs, R.string.dcim, 1)
            .addTab(LocalMediaFileListFragment.class, directoryMusicFileListArgs, R.string.music, 2)
            .addTab(LocalMediaFileListFragment.class, directoryMoviesFileListArgs, R.string.movies, 3)
            .addTab(LocalMediaFileListFragment.class, externalStorageFileListArgs, R.string.external_storage, 4);
    Environment.getRootDirectory();
    File[] externalFilesDirs = ContextCompat.getExternalFilesDirs(getActivity(),null);
    for (int i = 0; i < externalFilesDirs.length; i++) {
        File file = externalFilesDirs[i].getParentFile().getParentFile().getParentFile().getParentFile();

        if (file.getAbsolutePath().equals(externalStorage))
            continue;
        Bundle bundle = new Bundle();
        bundle.putString(LocalMediaFileListFragment.ROOT_PATH_LOCATION, file.getAbsolutePath());
        bundle.putParcelable(LocalMediaFileListFragment.SORT_METHOD, sortMethod);

        tabsAdapter.addTab(LocalMediaFileListFragment.class, bundle, file.getName(),i+2);
    }

    return tabsAdapter;
}
 
Example 9
Source File: DirPickerActivity.java    From EhViewer with Apache License 2.0 4 votes vote down vote up
@Override
public void onClick(@NonNull View v) {
    if (mDefault == v) {
        File[] externalFilesDirs = ContextCompat.getExternalFilesDirs(this, null);
        File[] dirs = new File[externalFilesDirs.length + 1];
        dirs[0] = AppConfig.getDefaultDownloadDir();
        for (int i = 0; i < externalFilesDirs.length; i++) {
            dirs[i + 1] = new File(externalFilesDirs[i], "download");
        }

        CharSequence[] items = new CharSequence[dirs.length];
        items[0] = getString(R.string.default_directory);
        for (int i = 1; i < items.length; i++) {
            items[i] = getString(R.string.application_file_directory, i);
        }

        new AlertDialog.Builder(this).setItems(items, (dialog, which) -> {
            File dir = dirs[which];
            if (!FileUtils.ensureDirectory(dir)) {
                Toast.makeText(DirPickerActivity.this, R.string.directory_not_writable, Toast.LENGTH_SHORT).show();
                return;
            }
            if (mDirExplorer != null) {
                mDirExplorer.setCurrentFile(dir);
            }
        }).show();
    } else if (mOk == v) {
        if (null == mDirExplorer) {
            return;
        }
        File file = mDirExplorer.getCurrentFile();
        if (!file.canWrite()) {
            Toast.makeText(this, R.string.directory_not_writable, Toast.LENGTH_SHORT).show();
        } else {
            Intent intent = new Intent();
            intent.setData(Uri.fromFile(file));
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}
 
Example 10
Source File: FileProvider.java    From EhViewer with Apache License 2.0 4 votes vote down vote up
/**
 * Parse and return {@link PathStrategy} for given authority as defined in
 * {@link #META_DATA_FILE_PROVIDER_PATHS} {@code <meta-data>}.
 *
 * @see #getPathStrategy(Context, String)
 */
private static PathStrategy parsePathStrategy(Context context, String authority)
        throws IOException, XmlPullParserException {
    final SimplePathStrategy strat = new SimplePathStrategy(authority);

    final ProviderInfo info = context.getPackageManager()
            .resolveContentProvider(authority, PackageManager.GET_META_DATA);
    final XmlResourceParser in = info.loadXmlMetaData(
            context.getPackageManager(), META_DATA_FILE_PROVIDER_PATHS);
    if (in == null) {
        throw new IllegalArgumentException(
                "Missing " + META_DATA_FILE_PROVIDER_PATHS + " meta-data");
    }

    int type;
    while ((type = in.next()) != END_DOCUMENT) {
        if (type == START_TAG) {
            final String tag = in.getName();

            final String name = in.getAttributeValue(null, ATTR_NAME);
            String path = in.getAttributeValue(null, ATTR_PATH);

            File target = null;
            if (TAG_ROOT_PATH.equals(tag)) {
                target = DEVICE_ROOT;
            } else if (TAG_FILES_PATH.equals(tag)) {
                target = context.getFilesDir();
            } else if (TAG_CACHE_PATH.equals(tag)) {
                target = context.getCacheDir();
            } else if (TAG_EXTERNAL.equals(tag)) {
                target = Environment.getExternalStorageDirectory();
            } else if (TAG_EXTERNAL_FILES.equals(tag)) {
                File[] externalFilesDirs = ContextCompat.getExternalFilesDirs(context, null);
                if (externalFilesDirs.length > 0) {
                    target = externalFilesDirs[0];
                }
            } else if (TAG_EXTERNAL_CACHE.equals(tag)) {
                File[] externalCacheDirs = ContextCompat.getExternalCacheDirs(context);
                if (externalCacheDirs.length > 0) {
                    target = externalCacheDirs[0];
                }
            }

            if (target != null) {
                strat.addRoot(name, buildPath(target, path));
            }
        }
    }

    return strat;
}